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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt sqlit3的增、删、改、查、判断等基本操作接口

發布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt sqlit3的增、删、改、查、判断等基本操作接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、Qt sqlit3簡介

Qt SQLite(sql)是一款不需要服務器的開源輕量級的數據庫軟件,可以集成在其他軟件中,適合嵌入式系統應用。Qt5以上版本直接支持SQLite。具體的特性和語法可以參考RUNOOB. 這里我把自己項目中用到的基本操作函數貼出來。

這里主要介紹操作接口:

  • 連接sqlit數據庫
  • 關閉sqlit數據庫
  • 判斷sqlit表格是否存在
  • 判斷是否表中某個字段存在某個值
  • 判斷sqlit表中是否存在sysid
  • sqlit新建表
  • sqlit插入項目
  • 刪除sqlit指定表中指定項目中指定的內容
  • 更新sqlit表中的內容
  • 2、使用方法

    在工程配置文件.pro中添加QT對sqlit的支持。

    QT += sql

    3、接口文件

    3.1 連接sqlit數據庫

    static bool connectMyDB() {QString dbName = "MysqliteDB";QString dbuser = "jianwang16";QString dbpassword = "password123456";if(QSqlDatabase::contains(dbName)){//如已經打開這個數據庫,直接調出這個數據連接database = QSqlDatabase::database(dbName);}else{//否則打開這個數據庫,注意帶上數據庫名database = QSqlDatabase::addDatabase("QSQLITE");database.setDatabaseName(dbName);database.setUserName(dbuser);database.setPassword(dbpassword);}if (!database.open()){QMessageBox::critical(NULL, "錯誤", " Unable to establish a database connection!!! ", QMessageBox::Cancel, QMessageBox::Cancel);qDebug() << "Error: Failed to connect database." << database.lastError();return false;}else{qDebug()<<"數據庫打開成功!";return true;}

    3.2 關閉sqlit數據庫

    static void closeMyDB() {database.close(); }

    3.3 判斷sqlit表格是否存在

    static bool isExistTable(const QString& strTableName) {if(connectMyDB()){QStringList tables = database.tables(); //獲取數據庫中的表qDebug() <<QString("表的個數: %1").arg(tables.count()); //打印表的個數QStringListIterator itr(tables);while (itr.hasNext()){QString tableName = itr.next();qDebug() << "表名:"+ tableName;if(tableName==strTableName)return true;}return false;} }

    3.4 判斷是否表中某個字段存在某個值

    static bool isExist(const QString& strTableName, const QString& strFieldName,const QString& text) {if(connectMyDB()){QSqlQuery query;QString strSql = QString("SELECT 1 FROM %1 WHERE %2 ='%3'").arg(strTableName).arg(strFieldName).arg(text);//select fromquery.prepare(strSql);if(query.exec()){qDebug()<<strSql<<"成功";query.next();qDebug()<<"返回內容"<<query.value(0).toString();if(query.value(0).toInt()){qDebug()<<"當前表中存在字段"<<text;return true;}else{qDebug()<<"當前表中不存在字段"<<text;return false;}}else{qDebug()<<strSql<<"失敗";}} }

    3.5?判斷sqlit表中是否存在sysid

    *說明:System_ID是我表中的字段。一下函數的意思是判斷我的某個表中System_ID是否存在這個sysid。

    static bool isExistSystemID(const QString& strTableName, const QString& sysid) {if(connectMyDB()){QSqlQuery query(database);QString sql = QString("select 1 from %1 where System_ID ='%2'").arg(strTableName).arg(sysid);query.prepare(sql);qDebug()<<sql;if(!query.exec()){qDebug()<<query.lastError();return false;}else{query.next();int nResult = query.value(0).toInt();//有此字段時返回1,無字段時返回nullqDebug()<<"query.value(0).toInt():"<<QString::number(query.value(0).toInt());query.clear();if(nResult){return true;}else return false;}}

    3.6 sqlit新建表

    static void creatTable(const QString& strTableName) {QSqlQuery query;if (!query.exec(QString("CREATE TABLE %1(""id INTEGER PRIMARY KEY AUTOINCREMENT,"//主鍵"System_ID VARCHAR,"//系統ID"User_ID VARCHAR,"//用戶ID"SubMeter_Num VARCHAR,"//子表序號"CMD_Time VARCHAR)").arg(strTableName))){qDebug() <<"Create"<< strTableName<<"Table Failed!";}else{qDebug() << "Create"<< strTableName<<"Table OK!";} }

    3.7 sqlit插入項目

    static void addIterm( const QString& strTableName,const QString& System_ID,const QString& User_ID,const QString& DTU_ID,const QString& SubMeter_Num) {QSqlQuery query(database);QString select_max_sql =QString("select max(id) from %1").arg(strTableName);qDebug()<<select_max_sql;//查詢最大idint max_id = 0;query.prepare(select_max_sql);if(!query.exec()){qDebug()<<"最大ID錯誤"<<query.lastError();}else{while(query.next()){max_id = query.value(0).toInt();qDebug()<<QString("max id:%1").arg(max_id);}}//綁定參數query.prepare(QString("INSERT INTO %1 VALUES(""?,"//id, ""?," // "System_ID, ""?,"// "User_ID,""?,"// "DTU_ID,""?"// "SubMeter_Num,"")").arg(strTableName));query.addBindValue(max_id+1);query.addBindValue(System_ID);query.addBindValue(User_ID);query.addBindValue(DTU_ID);query.addBindValue(SubMeter_Num);bool bResult = query.exec();qDebug() << "addIterm result=" << bResult;}

    3.8?刪除sqlit指定表中指定項目中指定的內容

    static bool deleteiterm(const QString& tablename, const QString& item, const QString & text) {if(connectMyDB()){QSqlQuery query(database);QString delete_sql = QString("delete from %1 where %2 = ?").arg(tablename).arg(item);query.prepare(delete_sql);query.addBindValue(text);if(!query.exec()){qDebug()<<query.lastError();return false;}else{qDebug()<<"deleted!";return true;}}else return false; }

    3.9 更新sqlit表中的內容

    static void updateitem( const QString& strTableName,const QString& systemid,const QString& User_ID,const QString& DTU_ID,const QString& SubMeter_Num) {if(connectMyDB()){QSqlQuery query(database);query.prepare(QString("UPDATE %1 SET ""User_ID = :User_ID,""DTU_ID = :DTU_ID,""SubMeter_Num = :SubMeter_Num,"WHERE System_ID = %2").arg(strTableName).arg(systemid));query.bindValue(":User_ID",User_ID);query.bindValue(":DTU_ID",DTU_ID);query.bindValue(":SubMeter_Num",SubMeter_Num);if(query.exec()){qDebug()<<"更新成功";}else{qDebug()<<"更新失敗";}} }

    ?

    總結

    以上是生活随笔為你收集整理的Qt sqlit3的增、删、改、查、判断等基本操作接口的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。