一、在.pro文件添加
QT += core gui sql
二、头文件
#include <QSqlDatabase> #include <QSqlError> #include <QSqlQuery>
三、用法
1 //以写的查询为例 2 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); 3 4 db.setDatabaseName("D://mycodetest//map81.db"); 5 6 if(!db.open()){ 7 qDebug()<<"fail"; 8 } 9 else{ 10 qDebug() << "success"; 11 } 12 13 QSqlQuery sqlQuery; 14 15 QString query_sql = "SELECT * FROM 'carmodel' LIMIT 0,30"; 16 17 if(!sqlQuery.exec(query_sql)){ 18 qDebug() << "exec fail"; 19 }else{ 20 if(sqlQuery.first()){//可以用while(sqlQuery.next())查询所有记录 21 QString name = sqlQuery.value(0).toString(); 22 QString points = sqlQuery.value(2).toString(); 23 //qDebug() << name << points; 24 datastr = points; 25 initcarmodel(name, datastr); 26 27 } 28 db.close();
标签:Sqlite,QT,db,用法,include,QSqlDatabase From: https://www.cnblogs.com/zxybdnb/p/17528722.html