通过Qt *** 作sqlite数据库。建库,建表,添加字段、增删改查。
先介绍下常用的SQL语句:
判断数据库中是否存在某个表: SELECT 1 FROM sqlite_master where type = 'table' and name = 'book';
判断表中是否存在某字段:SELECT 1 FROM sqlite_master where type = 'table' and name= 'book' and sql like '%IDtest%';
建表语句:CREATE table book(ID VARCHAR(20) PRIMARY KEY , name VARCHAR(100), info VARCHAR(100));
添加字段:ALTER table book ADD descinfo VARCHAR(100);
创建索引:CREATE INDEX book_index1 on book(ID);
添加:INSERT INTO book(ID, name, info) VALUES('1111', 'Qt编程', '这是一本好书');
删除:DELETE FROM book WHERE ID = '1111';
更新:UPDATE book SET name = 'test' WHERE ID = '1111';
精确搜索:SELECT * FROM book WHERE ID = '111';
模糊搜索:SELECT * FROM book WHERE name like '%:strBookname%' ESCAPE '!';
Qt项目的pro文件中需要添加sql支持。若采用VS开发Qt程序,需要在项目配置中添加Qtsql支持。
QT += core gui sql
代码实现主要函数(建库,建表,添加字段、增删改查):
//创建数据库文件bool DbManager::createDatafile(const QString &strfilename){ if(!Qfile::exists(strfilename))//文件不存在,则创建 { QDir fileDir = QfileInfo(strfilename).absoluteDir(); QString strfileDir = QfileInfo(strfilename).absolutePath(); if(!fileDir.exists()) //路径不存在,创建路径 { fileDir.mkpath(strfileDir); } Qfile dbfile(strfilename); if(!dbfile.open(qiodevice::writeonly))//未成功打开 { dbfile.close(); return false; } dbfile.close(); } return true;}//打开数据库文件(sqlite,*.db文件)bool DbManager::openDataBase(const QString& strfilename){ m_db = QsqlDatabase::addDatabase("QsqlITE"); m_db.setDatabasename(strfilename); if(m_db.open()) { return true; } return false;}//关闭数据库voID DbManager::closeDataBase(){ m_db.close();}//判断数据库中是否存在该表bool DbManager::isExisttable(const QString& strtablename){ Qsqlquery query; QString strsql = QString("SELECT 1 FROM sqlite_master where type = 'table' and name = '%1'").arg(strtablename); query.exec(strsql); if(query.next()) { int nResult = query.value(0).toInt();//有表时返回1,无表时返回null if(nResult) { return true; } } return false;}//判断表中是否含有某字段(列)bool DbManager::isExistFIEld(const QString& strtablename,const QString& strFIEldname){ Qsqlquery query(m_db); QString strsql = QString("SELECT 1 FROM sqlite_master where type = 'table'" " and name= '%1' and sql like '%%2%'").arg(strtablename).arg(strFIEldname); query.exec(strsql); if(query.next()) { int nResult = query.value(0).toInt();//有此字段时返回1,无字段时返回null if(nResult) { return true; } } return false;}
//创建表voID DbBook::createtable(){ QString strsql = "create table book(ID VARCHAR(20) PRIMARY KEY,name VARCHAR(100),info TEXT);"; Qsqlquery query(DbManager::m_db); query.exec(strsql);//建表 QString strIndexsql = "CREATE INDEX book_index1 on book(ID);"; bool bResult = query.exec(strIndexsql); //创建索引}//添加一图书voID DbBook::addBook(const QString& strBookID,const QString& strBookname,const QString& strBookInfo){ QString strsql; Qsqlquery query(DbManager::m_db); strsql.append("INSERT INTO book(ID,name,info) VALUES (:ID,:name,:info)"); //需要绑定的字段 query.prepare(strsql); query.bindValue(":ID",strBookID); query.bindValue(":name",strBookname); query.bindValue(":info",strBookInfo); bool bResult = query.exec(); qDeBUG() << "addBook,result=" << bResult;}//根据ID删除图书voID DbBook::removeBook(const QString& strBookID){ QString strsql = QString("DELETE FROM book WHERE ID = '%1'").arg(strBookID); Qsqlquery query(DbManager::m_db); bool bResult = query.exec(strsql); qDeBUG() << "removeBook,result=" << bResult;}//根据ID更新图书名voID DbBook::updateBookname(const QString& strBookID,const QString& strBookname){ QString strsql = "UPDATE book SET name = :name WHERE ID = :ID"; Qsqlquery query(DbManager::m_db); query.prepare(strsql); query.bindValue(":ID",strBookname); bool bReuslt = query.exec();}//根据图书名找图书(模糊搜索)QStringList DbBook::searchByBookname(const QString& strBookname){ QStringList bookIDList; Qsqlquery query(DbManager::m_db); QString strname = strBookname; strname.replace("%","!%"); const QString strsqlTemplate = "SELECT * FROM book WHERE name like '%:strBookname%' ESCAPE '!'";//SQL语句模板 QString strsql = strsqlTemplate; strsql.replace(":strBookname",strname); query.exec(strsql); while(query.next()) { QString strBookID = query.value(0).toString();//图书ID bookIDList.append(strBookID); } return bookIDList;}
演示程序下载地址
http://download.csdn.net/detail/lingyun0/8242033
By Lankin
2014/12/9
总结以上是内存溢出为你收集整理的基于Qt的sqlite编程全部内容,希望文章能够帮你解决基于Qt的sqlite编程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)