http://blog.csdn.net/liukang325/article/details/22735339
基本打开数据库,执行SQL语句:
- static const char* DBName = "./test.db";
- QFileInfo file(DBName);
- if(file.exists() == false)
- {
- QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
- db.setDatabaseName(DBName); //设置数据库名
- if (!db.open())
- {
- qDebug("数据库不能打开");
- return -1;
- }
- QSqlQuery query;
- ///创建卡号数据库
- query.exec("CREATE TABLE tb_card(cardNum varchar(64) PRIMARY KEY, name varchar(64), job varchar(64))");
- query.clear();
- }
select查询获取数据:
- QSqlQuery query(dbConn);
- QString sqlStr;
- sqlStr.append("select * from tb_card");
- bool isSuccess = query.exec(sqlStr);
- QString cardNum, name, job;
- int i = 0;
- while(query.next()) //query.next()指向查找到的第一条记录,然后每次后移一条记录
- {
- cardNum.clear();
- name.clear();
- job.clear();
- cardNum.append(query.value(0).toString());
- name.append(query.value(1).toString());
- job.append(query.value(2).toString());
- }
QT += core gui sql