Qt内置的QSQLITE数据库详解
发表时间: 2022-12-12 17:59
database.h
1 #ifndef DATABASE_H 2 #define DATABASE_H 3 4 #include <QTextCodec> 5 #include <QSqlDatabase> 6 #include <QSqlQuery> 7 #include <QTime> 8 #include <QSqlError> 9 #include <QtDebug>10 #include <QSqlDriver>11 #include <QSqlRecord>12 13 class DataBase14 {15 public:16 bool createConnection(); //创建一个连接17 bool createTable(); //创建数据库表18 bool insert(); //出入数据19 bool queryAll(); //查询所有信息20 bool updateById(int id); //更新21 bool deleteById(int id); //删除22 bool sortById(); //排序23 };24 25 #endif // DATABASE_H
database.cpp
1 #include "database.h" 2 3 4 //建立一个数据库连接 5 bool DataBase::createConnection() 6 { 7 //以后就可以用"sqlite1"与数据库进行连接了 8 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "sqlite1"); 9 db.setDatabaseName(".//qtDb.db"); 10 if( !db.open()) 11 { 12 qDebug() << "无法建立数据库连接"; 13 return false; 14 } 15 return true; 16 } 17 18 //创建数据库表 19 bool DataBase::createTable() 20 { 21 QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接 22 QSqlQuery query(db); 23 bool success = query.exec("create table automobil(id int primary key,attribute varchar," 24 "type varchar,kind varchar,nation int,carnumber int,elevaltor int," 25 "distance int,oil int,temperature int)"); 26 if(success) 27 { 28 qDebug() << QObject::tr("数据库表创建成功!\n"); 29 return true; 30 } 31 else 32 { 33 qDebug() << QObject::tr("数据库表创建失败!\n"); 34 return false; 35 } 36 } 37 38 //向数据库中插入记录 39 bool DataBase::insert() 40 { 41 QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接 42 QSqlQuery query(db); 43 query.prepare("insert into automobil values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 44 45 long records = 10; 46 for(int i=0; i<records; i++) 47 { 48 query.bindValue(0, i); 49 query.bindValue(1, "四轮"); 50 query.bindValue(2, "轿车"); 51 query.bindValue(3, "富康"); 52 query.bindValue(4, rand()%100); 53 query.bindValue(5, rand()%10000); 54 query.bindValue(6, rand()%300); 55 query.bindValue(7, rand()%200000); 56 query.bindValue(8, rand()%52); 57 query.bindValue(9, rand()%100); 58 59 bool success=query.exec(); 60 if(!success) 61 { 62 QSqlError lastError = query.lastError(); 63 qDebug() << lastError.driverText() << QString(QObject::tr("插入失败")); 64 return false; 65 } 66 } 67 return true; 68 } 69 70 //查询所有信息 71 bool DataBase::queryAll() 72 { 73 QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接 74 QSqlQuery query(db); 75 query.exec("select * from automobil"); 76 QSqlRecord rec = query.record(); 77 qDebug() << QObject::tr("automobil表字段数:" ) << rec.count(); 78 79 while(query.next()) 80 { 81 for(int index = 0; index < 10; index++) 82 qDebug() << query.value(index) << " "; 83 qDebug() << "\n"; 84 } 85 } 86 87 //根据ID删除记录 88 bool DataBase::deleteById(int id) 89 { 90 QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接 91 QSqlQuery query(db); 92 query.prepare(QString("delete from automobil where id = %1").arg(id)); 93 if(!query.exec()) 94 { 95 qDebug() << "删除记录失败!"; 96 return false; 97 } 98 return true; 99 }100 101 //根据ID更新记录102 bool DataBase::updateById(int id)103 {104 QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接105 QSqlQuery query(db);106 query.prepare(QString("update automobil set attribute=?,type=?,"107 "kind=?, nation=?,"108 "carnumber=?, elevaltor=?,"109 "distance=?, oil=?,"110 "temperature=? where id=%1").arg(id));111 112 query.bindValue(0,"四轮");113 query.bindValue(1,"轿车");114 query.bindValue(2,"富康");115 query.bindValue(3,rand()%100);116 query.bindValue(4,rand()%10000);117 query.bindValue(5,rand()%300);118 query.bindValue(6,rand()%200000);119 query.bindValue(7,rand()%52);120 query.bindValue(8,rand()%100);121 122 bool success=query.exec();123 if(!success)124 {125 QSqlError lastError = query.lastError();126 qDebug() << lastError.driverText() << QString(QObject::tr("更新失败"));127 }128 return true;129 }130 131 //排序132 bool DataBase::sortById()133 {134 QSqlDatabase db = QSqlDatabase::database("sqlite1"); //建立数据库连接135 QSqlQuery query(db);136 bool success=query.exec("select * from automobil order by id desc");137 if(success)138 {139 qDebug() << QObject::tr("排序成功");140 return true;141 }142 else143 {144 qDebug() << QObject::tr("排序失败!");145 return false;146 }147 }
main.cpp
【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】
点击→领取「链接」
1 #include <QCoreApplication> 2 #include "database.h" 3 4 int main(int argc, char *argv[]) 5 { 6 QCoreApplication a(argc, argv); 7 QTextCodec::setCodecForLocale(QTextCodec::codecForLocale()); 8 9 DataBase d;10 d.createConnection(); //创建连接11 //d.createTable();12 //d.insert();13 d.queryAll(); //已经完成过createTable(), insert(), 现在进行查询14 15 return 0;16 }
运行结果
项目名称为”sqlex”,然后就可以在与当前项目同一级目录下的“
build-SQLEx-Desktop_Qt_5_7_0_MinGW_32bit-Debug”中可以看到qtDb.db数据库文件,该数据库可以用软件SQLite直接打开