日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

【Qt】sqlite数据库使用

發(fā)布時間:2025/3/20 数据库 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Qt】sqlite数据库使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、修改.pro文件,添加SQL模塊:

QT += sql

2.maindow.h

#include "mainwindow.h" #include <QApplication> //添加頭文件 #include <qdebug.h> #include <QSqlDatabase> #include <QSqlError> #include <QSqlQuery>int main(int argc, char *argv[]) {QApplication a(argc, argv);//建立并打開數(shù)據(jù)庫QSqlDatabase database;database = QSqlDatabase::addDatabase("QSQLITE");database.setDatabaseName("MyDataBase.db"); //通過QCoreApplication::applicationDirPath()獲得相對路徑/*QString PATH;PATH.append(QCoreApplication::applicationDirPath());PATH.append("/databaseUser.db");QSqlDatabase database;database = QSqlDatabase::addDatabase("QSQLITE");database.setDatabaseName(PATH);*///if (!database.open()){qDebug() << "Error: Failed to connect database." << database.lastError();}else{qDebug() << "Succeed to connect database." ;}//創(chuàng)建表格QSqlQuery sql_query;if(!sql_query.exec("create table student(id int primary key, name text, age int)")){qDebug() << "Error: Fail to create table."<< sql_query.lastError();}else{qDebug() << "Table created!";}//插入數(shù)據(jù)if(!sql_query.exec("INSERT INTO student VALUES(1, \"Wang\", 23)")){qDebug() << sql_query.lastError();}else{qDebug() << "inserted Wang!";}if(!sql_query.exec("INSERT INTO student VALUES(2, \"Li\", 23)")){qDebug() << sql_query.lastError();}else{qDebug() << "inserted Li!";}//修改數(shù)據(jù)sql_query.exec("update student set name = \"QT\" where id = 1");if(!sql_query.exec()){qDebug() << sql_query.lastError();}else{qDebug() << "updated!";}//查詢數(shù)據(jù)sql_query.exec("select * from student");if(!sql_query.exec()){qDebug()<<sql_query.lastError();}else{while(sql_query.next()){int id = sql_query.value(0).toInt();QString name = sql_query.value(1).toString();int age = sql_query.value(2).toInt();qDebug()<<QString("id:%1 name:%2 age:%3").arg(id).arg(name).arg(age);}}//刪除數(shù)據(jù)sql_query.exec("delete from student where id = 1");if(!sql_query.exec()){qDebug()<<sql_query.lastError();}else{qDebug()<<"deleted!";}//刪除表格sql_query.exec("drop table student");if(sql_query.exec()){qDebug() << sql_query.lastError();}else{qDebug() << "table cleared";}//關(guān)閉數(shù)據(jù)庫database.close();return a.exec(); }

總結(jié)

以上是生活随笔為你收集整理的【Qt】sqlite数据库使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。