#include <QtCore/QCoreApplication>
#include <QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("/mnt/sdcard/test.db");
if(!db.open())
{
return false;
}
printf("open OK\n");
QSqlQuery query;
query.exec("creat table student(id int primary key,name varchar)");
query.exec("insert into student values(1,'xiaofang')");
query.exec("insert into student values(2,'xiaohu')");
query.exec("insert into student values(3,'xiaozhu')");
query.exec("select id from student where id >=2");
printf("mark1\n");
while(query.next())
{
int ele0 = query.value(0).toInt();
QString ele1=query.value(1).toString();
qDebug() << ele0 <<ele1;
printf("\n%s\n",ele0);
printf("\n%s\n",ele1);
printf("mark2\n");
}
printf("mark3\n");
return a.exec();
}
以上是我按照网上的例子的代码,运行后发现没有进while()循环,生成了test.db的文件,可是大小为0 K字节?请问是什么原因呢?代码不知道是不是正确的,帮忙看看!
------解决方案--------------------
query.exec("creat table student(id int primary key,name varchar)");
create,错误是这。