QT5中使用SQLite的實(shí)現(xiàn)方法
SQLite(sql)是一款開源輕量級(jí)的數(shù)據(jù)庫軟件,不需要server,可以集成在其他軟件中,非常適合嵌入式系統(tǒng)。
Qt5以上版本可以直接使用SQLite。
1、修改.pro文件,添加SQL模塊:
QT += sql
2、main.cpp代碼如下:
#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");
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();
}
3、應(yīng)用程序輸出如下:

4、創(chuàng)建的 MyDataBase.db 在build的這個(gè)文件夾下:
D:\QT\project\build-sl-Desktop_Qt_5_10_1_MinGW_32bit-Debug
到此這篇關(guān)于QT5中使用SQLite的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)QT5使用SQLite內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)餐飲點(diǎn)餐管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)餐飲點(diǎn)餐管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
VS Code 中搭建 Qt 開發(fā)環(huán)境方案分享
這篇文章主要介紹了VS Code 中搭建 Qt 開發(fā)環(huán)境方案分享的相關(guān)資料,需要的朋友可以參考下2022-12-12
C++如何通過ostringstream實(shí)現(xiàn)任意類型轉(zhuǎn)string
再使用整型轉(zhuǎn)string的時(shí)候感覺有點(diǎn)棘手,因?yàn)閕toa不是標(biāo)準(zhǔn)C里面的,而且即便是有itoa,其他類型轉(zhuǎn)string不是很方便。后來去網(wǎng)上找了一下,發(fā)現(xiàn)有一個(gè)好方法2013-09-09
opencv3機(jī)器學(xué)習(xí)之EM算法示例詳解
這篇文章主要介紹了opencv3機(jī)器學(xué)習(xí)之EM算法的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
C++實(shí)現(xiàn)簡(jiǎn)單FTP客戶端軟件開發(fā)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單FTP客戶端軟件開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C語言中的結(jié)構(gòu)體內(nèi)嵌函數(shù)用法
這篇文章主要介紹了C語言中的結(jié)構(gòu)體內(nèi)嵌函數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Qt 使用Poppler實(shí)現(xiàn)pdf閱讀器的示例代碼
下面小編就為大家分享一篇Qt 使用Poppler實(shí)現(xiàn)pdf閱讀器的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01

