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()<<"更新失败";}}
}