16,173
社区成员
发帖
与我相关
我的任务
分享
#include <QtCore/QCoreApplication>
//#include "database.h"
#include<QtSql>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlQuery>
#include <QStringList>
//#include<QMessageBox>
#include<QSqlError>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
//添加数据库驱动
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers)
qDebug() << driver;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
//设置数据库名称
db.setDatabaseName(":memory:");qDebug()<<"inserted!1";
//打开数据库
if(!db.open())
{
qDebug()<<db.lastError();
return false;
}
//以下执行相关sql语句
QSqlQuery query;
//新建student表,id设置为主键,还有一个name项
query.exec("create table student(id int primary key,name varchar)");
//向表中插入3条记录
query.exec("insert into student values(1,'xiaogang')");
query.exec("insert into student values(2,'xiaoming')");
query.exec("insert into student values(3,'xiaohong')");
//查找表中id >=2 的记录的id项和name项的值
query.exec("select id,name from student where id >= 2");
//query.next()指向查找到的第一条记录,然后每次后移一条记录
while(query.next())
{
//query.value(0)是id的值,将其转换为int型
int value0 = query.value(0).toInt();
QString value1 = query.value(1).toString();
//输出两个值
qDebug() << value0 << value1 ;
}
return a.exec();
}