Qt 数据库(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lewanhah01/article/details/50628649

下面是Qt数据库操作的实例,是从其他的博客的整理归纳!仅供参考!

/*包含一些必要的头文件*/

#include<QApplication>
#include<iostream>
#include<QtSql>
#include<QSqlDatabase>

int main(int argc,char *argv[])
{
    QApplication app(argc,argv);
   
/*选择数据库的类型*/
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

  /*为新建数据库命名*/
    db.setDatabaseName("zhongqujiats.db");
    /*设置用户名*/

    db.setUserName("zqj");

  /*设置用户密码*/
    db.setPassword("123456");
    if(!db.open()){ //执行打开操作,如果打开失败open函数返回0,则
       qDebug()<<db.lastError();               //lastErro函数返回上次错误的消息,并输出值qDebug显示
       printf("db: failed to connect \n");
       return false;
    }
    QSqlQuery query; //QSqlQuery类提供值执行SQL命令的手段

    query.exec("create table student (id int primary key, name varchar(20), age int)"); /*执行SQL命令,命令以字符串方式作为参数传入。*/

    /* :id,:name,:age 是占位符 ,暂不赋值,只是给值留下位置*/
    query.prepare("insert into student (id,name,age)" "values(:id, :name, :age)");
    query.bindValue(":id",70); //bindValue为赋值函数,第一个参数表示给哪个段赋值,相识的函数还addBindValue
    query.bindValue(":name","xiaoli");
    query.bindValue(":age",25);
    query.exec();


    /* 这是另一风格的赋值方式, ?是  占位符*/
    query.prepare("insert into student (id,name,age) values(?,?,?)");
    query.exec();
    query.addBindValue(71); //赋值要求按照字段的顺序
    query.addBindValue("xiaohe");
    query.addBindValue(24);
    query.exec();


    query.prepare("insert into student (id,name,age) values(?,?,?)");
    query.bindValue(0,72); //bindValue第一个参数无论是序号还是占位符都是可行的
    query.bindValue(1,"xiaohe");
    query.bindValue(2,24);
    query.exec();


    query.prepare("insert into student (id,name,age) values(73,'xiaoyang',25)");
    query.exec();
  prepare函数是准备命令并不执行,真正执行的是exec函数,上面的语句也可以写成这样:

query.exec("insert into student (id,name,age) values(73,'xiaoyang',25)");两者效果是一样的,prepare函数一次只能准备一条命令。

如果像上面这么写,当数据多时可能会显得代码很长,下面的方法提供了批量处理
    /* 批量处理  */
    query.prepare("insert into student (id,name,age) values(?,?,?)");
    QVariantList ids;
    ids << 68<< 69;
    QVariantList names;
    names << "husheng" << "liuzheng";
    QVariantList ages;
    ages<<19<<30;
    query.addBindValue(ids);
    query.addBindValue(names);
    query.addBindValue(ages);
    query.execBatch();
查看下ececBatch()的文档。大概意思是批处理的预先准备

/*

扫描二维码关注公众号,回复: 3246394 查看本文章

Executes a previously prepared SQL query in a batch. All the bound parameters have to be lists of variants. If the database doesn't support batch executions, the driver will simulate it using conventional exec() calls.

*/

    if(!query.isActive()){     //用来判断执行上面的命令是否成功
       qDebug()<<query.lastError().text();
       printf("query: execute error1\n");
    }
    query.setForwardOnly(true);   //设置只能向前查询结果集,只可使用seek和next函数
    query.exec("select * from student");
    if(!query.isActive()){
        qDebug()<<query.lastError().text();
        printf("query: execute error2\n");
    }
    while(query.next()){   //每执行执行一次,便会指向小一笑结果消息,默认从0开始,也可以使用seek定位
        int id = query.value(0).toInt();   //取出结果消息的第一个字段,并转换为int类型,对应id
        QString name = query.value(1).toString();
        int age = query.value(2).toInt();
        std::cout<<qPrintable(name)<<": "<<id<<": "<<age<<std::endl;  
    }
    /* clear database 清楚表内容,格式/样式不清除*/ 
    query.exec("delete from student");
    /* colse database 关闭数据库*/
    db.close();
    /* delete database 删除数据库*/
    QFile::remove("database.db");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lewanhah01/article/details/50628649