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++中new與delete、malloc與free應(yīng)用分析
這篇文章主要介紹了C++中new與delete、malloc與free應(yīng)用分析,很重要的概念,需要的朋友可以參考下2014-08-08Opencv2.4.9函數(shù)HoughLinesP分析
這篇文章主要為大家詳細(xì)介紹了Opencv2.4.9函數(shù)HoughLinesP,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01C語言中操作utmp文件的相關(guān)函數(shù)用法
這篇文章主要介紹了C語言中操作utmp文件的相關(guān)函數(shù)用法,包括getutent()函數(shù)和setutent()函數(shù)以及endutent()函數(shù),需要的朋友可以參考下2015-08-08字符串拷貝函數(shù)memcpy和strncpy以及snprintf 的性能比較
以下是對(duì)字符串拷貝函數(shù)memcpy和strncpy以及snprintf它們之間的性能進(jìn)行了比較,需要的朋友可以過來參考下2013-07-07C++非繼承時(shí)函數(shù)成員訪問屬性和類繼承過程中的訪問控制
這篇文章主要介紹了C++非繼承時(shí)函數(shù)成員訪問屬性和類繼承過程中的訪問控制,非繼承時(shí),protected成員和private成員沒有任何區(qū)別,都是類內(nèi)部可以直接訪問它們、類外部的類對(duì)象不可訪問它們、類內(nèi)部的類對(duì)象可以訪問它們,更多詳細(xì)內(nèi)容請(qǐng)參考下面相關(guān)資料2022-03-03