Qt连接本地数据库后,进行查询和增加数据库表的字段Demo

#include <QCoreApplication>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlQuery>

//-- 连接本地数据库
bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("test"); //-- 数据库名字
    db.setUserName("root");
    db.setPassword("123456");
    if(!db.open())
    {
        printf("-------\n");
        return false;
    }
    return true;
}

#include <iostream>
#include <QDateTime>
using namespace std;
//-- 查看数据库 spaceobjectorbitdata 的表中,满足条件的数据
bool SearchData(int & id)
{
    QSqlQuery query;
    //--  写入存储语句,就能查询到数据库的内容了
    query.exec("SELECT epc,orb_sma From spaceobjectorbitdata where orb_inc>105");

    //-- 检查是否有错误发生
    if(query.isActive())
    {

        //-- 如果没有错误发生查询会变成激活状态,可以进行遍历结果集
        while (query.next()) {
            //-- value函数把字段值作为QVariant返回
            QDateTime title = query.value(0).toDateTime();
            qDebug() << title;
            int v = query.value(1).toInt();
            std::cerr << qPrintable(query.value(0).toString())
                      << "  :  "
                      << v
                      << std::endl;
            id++;
        }
    }
    else
    {
         qDebug() << "DataBase error";
    }

}

//-- 查询数据库中spaceobjectorbitdata的表中 最后一条数据
int lastdata()
{
    QSqlQuery query;
    //--  写入存储语句,就能查询到数据库的内容了
    query.exec("SELECT dat_id From spaceobjectorbitdata");

    int v = 0;
    //-- 检查是否有错误发生
    if(query.isActive())
    {
        //-- 如果没有错误发生查询会变成激活状态,可以进行遍历结果集
        query.last();
        //-- value函数把字段值作为QVariant返回
        v = query.value(0).toInt();
        std::cerr << v
                  << std::endl;
    }
    return v;
}

//-- 在数据库中插入数据
bool IsSertData(int id)
{
    //QString value = "VALUES " + id ",9, 8,'2022-04-06T12:58:20.000',10.1, 100.0, 1000.8, 5.0, 1.0, 20.0, 0.12, '2022-04-06T12:58:20.000', '2022-04-06T12:58:20.000')";

    QString value = QString("%1 %2 (%3 %4")
            .arg("INSERT INTO spaceobjectorbitdata (dat_id,so_id,ori,epc,orb_sma,orb_inc,orb_ecc,orb_ma,orb_ap,orb_raan,root,ctm,utm)")
            .arg("VALUES")
            .arg(id)
            .arg( ",7, 8,'2022-04-06T12:58:20.000',10.1, 100.0, 1000.8, 5.0, 1.0, 20.0, 0.12, '2022-04-06T12:58:20.000', '2022-04-06T12:58:20.000')");

    qDebug() << value;
    QSqlQuery query(
                   value);


//    QSqlQuery query("INSERT INTO tta (tt, tt1, tt2)"
//                    "VALUES (120, 5, '2022-04-06T12:58:20.000')");
    //-- 检查是否有错误发生
    if(query.isActive())
    {
        qDebug() << "Insert Data is ok";
    }
    qDebug() << "返回收Sql语句影响的行数,影响几行数据呢?:" << query.numRowsAffected() << endl;
}
//-- 指定占位符,绑定想要插入的数值
bool insertDataUsedPerpare()
{
    QSqlQuery q;
    q.prepare("INSERT INTO tta (tt, tt1, tt2)"
              "VALUES (:tt, :tt1, :tt2)");
    q.bindValue(":tt", 122);
    q.bindValue(":tt1",  5);
    q.bindValue(":tt2","2022-04-06T12:58:20.000");
    q.exec();
    int tmp1 = 1000;
    QString tmpS1 = "2022-03-06T12:58:20.000";
    q.bindValue(":tt", tmp1);
    q.bindValue(":tt1",  5);
    q.bindValue(":tt2",tmpS1);
    q.exec();
    qDebug() << "返回收Sql语句影响的行数,影响几行数据呢?:" << q.numRowsAffected() << endl;
}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QStringList drivers = QSqlDatabase::drivers();  //获取现在可用的数据库驱动
    //-- 我用的QMYSQL,如果打印输出没有,则需要自己配置驱动
    foreach(QString driver, drivers)
    qDebug() << driver;


    #ifdef Q_PROCESSOR_X86_32
        printf("----32 bit\n");
    #else
        printf("----64 bit\n");
    #endif

    if(!createConnection())
    {
        //32位就是4 ,64 位就是8
        int * pi= NULL;
        printf("%d", sizeof( pi ));
        printf("game over\n");
        return 1;
    }
    else
    {
        printf("Ok Ok\n");
    }
    int id = 11;

//-- 测试代码1
//    SearchData(id);

    printf("Hello World\n");
//-- 测试代码2
//    id = lastdata();
//    if(id !=0)
//    {
//      id += 1;
//      IsSertData(id);
//    }

//-- 测试代码3
    insertDataUsedPerpare();

    return a.exec();
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值