• QT sqlite的简单用法


    1、相关头文件

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include

    2、数据库对象

    QSqlDatabase database;

    3、创建数据库并打开数据库

    1. bool initDataBase()
    2. {
    3. if(QSqlDatabase::contains("qt_sql_default_connect")){
    4. database = QSqlDatabase::database("qt_sql_default_connect");
    5. }else{
    6. database = QSqlDatabase::addDatabase("QSQLITE");
    7. database.setDatabaseName("myDatabase.db");
    8. database.setUserName("hs");
    9. database.setPassword("hs");
    10. if(database.isValid()){
    11. qDebug() << "valid." ;
    12. }else{
    13. qDebug() << "not valid!"<< database.lastError();
    14. return false;
    15. }
    16. if (!database.open())
    17. {
    18. qDebug() << "Error: Failed to connect database." << database.lastError();
    19. return false;
    20. }
    21. }
    22. return true;
    23. }

    4、创建表

    1. bool testCreateTable()
    2. {
    3. QSqlQuery sql_query;
    4. QString create_sql = "create table student (id int primary key, name varchar(30), age int)";
    5. sql_query.prepare(create_sql);
    6. if(!sql_query.exec())
    7. {
    8. qDebug() << "Error: Fail to create table." << sql_query.lastError();
    9. return false;
    10. }
    11. else
    12. {
    13. qDebug() << "Table created!";
    14. }
    15. return true;
    16. }

    5、向表中插入数据

    1. bool testInsert(int id,const QString& name,int age)
    2. {
    3. QSqlQuery query;
    4. query.prepare("INSERT INTO student(id,name,age)"
    5. "VALUES (:id,:name,:age)");
    6. query.bindValue(":id",id);
    7. query.bindValue(":name",name);
    8. query.bindValue(":age",age);
    9. return query.exec();
    10. }

    6、查询表中的数据

    1. bool testQuery()
    2. {
    3. QSqlQuery sql_query;
    4. QString select_sql = "select id, name,age from student";
    5. if(!sql_query.exec(select_sql))
    6. {
    7. qDebug()<lastError();
    8. return false;
    9. }
    10. else
    11. {
    12. while(sql_query.next())
    13. {
    14. int id = sql_query.value("id").toInt();
    15. QString name = sql_query.value("name").toString();
    16. int age = sql_query.value("age").toInt();
    17. qDebug()<<QString("id:%1 name:%2 age:%3").arg(id).arg(name).arg(age);
    18. }
    19. }
    20. QString lastQuery = sql_query.lastQuery();
    21. qDebug() << "lastQuery: " << lastQuery;
    22. return true;
    23. }

    7、修改表中的数据

    1. bool testModify(const QString& name,int age,int id)
    2. {
    3. QString sql = "update student set name=:name,age=:age where id=:id";
    4. QSqlQuery query;
    5. query.prepare(sql);
    6. // 设置参数
    7. query.bindValue(":name",name);
    8. query.bindValue(":age",age);
    9. query.bindValue(":id",id);
    10. return query.exec();
    11. }

    8、删除表中的数据

    1. bool testDelete(int id)
    2. {
    3. QString sql = "delete from student where id=:id";
    4. QSqlQuery query;
    5. query.prepare(sql);
    6. // 设置参数
    7. query.bindValue(":id",id);
    8. return query.exec();
    9. }

    9、查询数据库中的表

    1. bool testQueryTable()
    2. {
    3. // 查询sqlite_master表
    4. QString querySql = "SELECT name, type FROM sqlite_master WHERE type IN ('table', 'view')";
    5. QSqlQuery query(querySql);
    6. // 遍历查询结果
    7. while (query.next()) {
    8. QString name = query.value(0).toString();
    9. QString type = query.value(1).toString();
    10. qDebug() << "------------------";
    11. qDebug() << "Object name:" << name;
    12. qDebug() << "Object type:" << type;
    13. qDebug() << "------------------";
    14. }
    15. return true;
    16. }

    10、删除指定的表

    1. bool testDeleteTable()
    2. {
    3. QSqlQuery sql_query;
    4. QString drop_sql = "DROP TABLE student";
    5. sql_query.prepare(drop_sql);
    6. if(!sql_query.exec())
    7. {
    8. qDebug() << sql_query.lastError();
    9. return false;
    10. }
    11. else
    12. {
    13. qDebug() << "drop table success";
    14. }
    15. return true;
    16. }

    11、查询表是否存在

    1. bool isTableExist()
    2. {
    3. QSqlQuery sql_query;
    4. // count(*) 表示查询数量,因为student只有一个,因此只查询了一行结果
    5. sql_query.exec("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='student'");
    6. int count = 0;
    7. if (sql_query.next()) {
    8. count = sql_query.value(0).toInt();
    9. }
    10. return count;
    11. }

    12、查询表的字段等

    1. void testRecord()
    2. {
    3. QSqlQuery query("select * from student");
    4. //QString sql = "select * from student";
    5. //query.prepare(sql);
    6. QSqlRecord rec = query.record();
    7. qDebug() << "Number of columns: " << rec.count();
    8. for(int i = 0; i < rec.count();++i){
    9. qDebug() << "field: " << rec.fieldName(i);
    10. }
    11. int nameCol = rec.indexOf("name"); // index of the field "name"
    12. while (query.next())
    13. qDebug() << query.value(nameCol).toString(); // output all names
    14. }

    13、查询数据库的一些其他输出信息

    1. void testDatabaseOutput()
    2. {
    3. QString connectionOptions = database.connectOptions();
    4. qDebug() <<"option:" << connectionOptions;
    5. QStringList connectionName = database.connectionNames();
    6. qDebug() << "connectionNames:"<
    7. qDebug() << "database Name: " << database.databaseName();
    8. qDebug() << "dirver Name: " << database.driverName();
    9. //QSqlDriver* driver = database.driver();
    10. qDebug() << "dirvers: " << database.drivers();
    11. qDebug() << "host Name: " << database.hostName();
    12. qDebug() << "password: " << database.password();
    13. qDebug() << "port: " << database.port();
    14. qDebug() << "tables: " << database.tables(QSql::TableType::AllTables);
    15. QSqlIndex rlt = database.primaryIndex("student");
    16. qDebug() << "primary key: "<name();
    17. QSqlRecord rec = database.record("student");
    18. qDebug() << "record count: "<count();
    19. for(int i = 0; i < rec.count();++i){
    20. qDebug() << "rec: " << rec.fieldName(i);
    21. qDebug() << "rec Value:" << rec.value(i).toString();
    22. QSqlField field = rec.field(i);
    23. qDebug() <<"field: "<< field.name();
    24. qDebug() << "field value: " << field.value().toString();
    25. qDebug() << "field type: " << field.type();
    26. }
    27. }

  • 相关阅读:
    PE文件解析(5):重定位表详解
    离线数据仓库建设
    多重定义的全局符号,链接器会如何链接的情况
    [python] 向量检索库Faiss使用指北
    关于400G光模块的常见问题解答
    Python教程之字典(Dictionary)操作详解
    Excel_VBA编程
    计算机毕业设计 SSM校园二手交流系统 大学二手交易平台系统 二手物品交易平台Java Vue MySQL数据库 远程调试 代码讲解
    Boot 连接 Impala数据库
    裁员趋势下的大厂面试:“字节跳动”
  • 原文地址:https://blog.csdn.net/qq_32285693/article/details/133706058