Qt开发之路——SQlite的使用(简单粗暴)
作者:互联网
SQlite是一个轻便的数据库,在Qt中有内置驱动,无需安装即可使用,非常方便。本文内容包括数据库的建创建,连接,打开,数据表的创建,数据表内容的增删查操作。涉及的代码均为自己做的项目中的,没有考虑代码屯余问题,注释清晰,方便学习理解,有任何问题可以留言,共同进步~
ok,上才艺~
首先
QT += sql
数据库的创建,连接和打开
qt_sql_default_connection是数据库默认名
void Widget::createDatabase(){
//create and connect the database
QSqlDatabase database;
if (QSqlDatabase::contains("qt_sql_default_connection"))
{
database = QSqlDatabase::database("qt_sql_default_connection");
}
else
{
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName("MyDataBase.db");
}
//open the database
if (!database.open())
{
qDebug() << "Error: Failed to connect database." << database.lastError();
}
else
{
qDebug() <<"数据库连接成功!"<<"从数据库读取数据.";
}
}
数据库准备,数据表创建,插入数据
操作表之前要做好数据库的准备,这里的数据插入是我项目中涉及到的一个treewidget中的数据,当然可以直接插入数据,仅供参考
void Widget::writeDatabase()
{
//create and connect database
QSqlDatabase database;
if (QSqlDatabase::contains("qt_sql_default_connection"))
{
database = QSqlDatabase::database("qt_sql_default_connection");
}
else
{
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName("MyDataBase.db");
}
//create table
QSqlQuery sql_query;//set id text primary key
//judge table exist or not
bool isTableExist = \
sql_query.exec(QString("select count(*) from realdata where type='table' and name='%1'").arg(("realdata")));
if(!isTableExist){
if(!sql_query.exec("create table realdata(id text,manufacturer text,powerstate text,serveritynum text,uuid text,health text)"))
{
qDebug() << "Error: Fail to create table."<< sql_query.lastError();
}
else
{
qDebug() << "Table created!";
}
}
else{
}
//ergodic the treewidget item,insert the data into database
QTreeWidgetItemIterator it(ui->treeWidget->currentItem());
while(*it){
(*it)->setHidden(false);
QTreeWidgetItem *item = *it;
if(!sql_query.exec(QString("INSERT INTO realdata VALUES('%1','%2','%3','%4','%5','%6')").arg(item->text(0)).arg(item->text(1)).arg(item->text(2)).arg(item->text(3)).arg(item->text(4)).arg(item->text(5))))
{
qDebug() << "insert !!" <<sql_query.lastError();
}
else
{
qDebug() << "inserted FirstItemData!";
}
++it;
}
//close database
database.close();
}
删除整个数据表
操作表之前要做好数据库的准备,本函数可用于清楚所有数据,类似于一键删除的功能
void Widget::deleteRealData(){
//create and connect database
QSqlDatabase database;
if (QSqlDatabase::contains("qt_sql_default_connection"))
{
database = QSqlDatabase::database("qt_sql_default_connection");
}
else
{
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName("MyDataBase.db");
}
//delete the table
QSqlQuery sql_query;
sql_query.exec("drop table realdata");
if(sql_query.exec())
{
qDebug() << sql_query.lastError();
}
else
{
qDebug() << "table cleared";
}
}
查看数据
查看数据之后是对treewidget的使用,仅供参考
void Widget::readRealData(){
createDatabase();
//read data
QSqlQuery sql_query;
sql_query.exec("select * from realdata");
if(!sql_query.exec())
{
qDebug()<<sql_query.lastError();
}
else
{
qDebug()<<"表:";
//read database
while(sql_query.next())
{
QString id = sql_query.value(0).toString();
QString manufacturer= sql_query.value(1).toString();
QString powerstate = sql_query.value(2).toString();
QString serveritynum = sql_query.value(3).toString();
QString uuid = sql_query.value(4).toString();
QString health = sql_query.value(5).toString();
qDebug()<<QString("id:%1 manufacturer:%2 powerstate:%3 serveritynum:%4 uuid:%5 health:%6").arg(id).arg(manufacturer).arg(powerstate).arg(serveritynum).arg(uuid).arg(health);
QStringList strs;
//
strs<<QString(id)<<QString(manufacturer)<<QString(powerstate)<<QString(serveritynum)<<QString(uuid);
//add one line for treewidget,content is strs
QTreeWidgetItem *strsroot = new QTreeWidgetItem(ui->treeWidget,strs);
//set the header to appropriate size
QHeaderView *head=ui->treeWidget->header();
head->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->treeWidget->setCurrentItem(strsroot);//set currentitem
}
qDebug()<<"成功显示在界面上.";
}
}
走过的冤枉路——SQL语句
一定要注意一点!!!sql语句中用到arg时,%1要带‘’,也就是’%1’,’%2’,否则会报错,QSqlError(“1“, “Unable to execute statement“, “near \“.73\“: syntax error“)
参考
这两篇参考写的比较好,可以看一看
https://blog.csdn.net/weixin_41656968/article/details/80473137
https://www.cnblogs.com/xia-weiwen/archive/2017/05/04/6806709.html
标签:SQlite,Qt,database,text,粗暴,sql,qDebug,query,QSqlDatabase 来源: https://blog.csdn.net/qq_16488989/article/details/112007338