qt中调用sqlite3出错了,while循环进不去 5
#include<QtSql>#include<QSqlQuery>#include<QtSql/QSqlDatabase>#include<QtSql/QSqlQuer...
#include <QtSql> #include<QSqlQuery>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
Sqlite_test::Sqlite_test(QWidget *parent) :QWidget(parent)
{
open_sqlite_pushbutton=new QPushButton("打开");
messageEdit=new QLineEdit();
QVBoxLayout *vboxlayout=new QVBoxLayout;
messageEdit2=new QLineEdit();
vboxlayout->addWidget(open_sqlite_pushbutton);
vboxlayout->addWidget(messageEdit);
vboxlayout->addWidget(messageEdit2);
setLayout(vboxlayout);
connect(open_sqlite_pushbutton,SIGNAL(clicked()),this,SLOT(open_sqlite()));
}
void Sqlite_test::open_sqlite()
{
QSqlDatabase dbconn=QSqlDatabase::addDatabase("QSQLITE");
dbconn.setDatabaseName("mytest1.db"); //在工程目录新建一个mytest.db的文件
if(!dbconn.open())
{
qDebug()<<"DB open failed!\n";
return;
}
printf("i am here ********\n");
QSqlQuery query(dbconn);//以下执行相关QSL语句
printf("i am here 222 ********\n");
query.exec("create table student(id varchar,name varchar)"); printf("i am here 333********\n");
//新建student表,id设置为主键,还有一个name项 query.exec(QObject::tr("insert into student values(1,'a')"));
query.exec(QObject::tr("insert into student values(2,'b')"));
query.exec(QObject::tr("insert into student values(3,'c')"));
query.exec(QObject::tr("insert into student values(3,'d')"));
query.exec(QObject::tr("insert into student values(4,’e')"));
query.exec(QObject::tr("insert into student values(5,'f')"));
query.exec("select id,name from student where id>=1");
printf("i am here 44444********\n");
if(!query.first())
{
qDebug()<<query.lastError().text();
}
while(query.next())//query.next()指向查找到的第一条记录,然后每次后移一条记录{
printf("i am here 555********\n");
int ele0=query.value(0).toInt();printf("i am here 666 ********\n");//query.value(0)是id的值,将其转换为int型
printf("i am here 777********\n");
qDebug()<<ele0<<ele1;printf("i am here 888********\n");//输出两个值
}
printf("i am here 9999********\n");
printf("ele0=%d\n",ele0);
}
query.exec(QObject::tr("drop student"));
} 展开
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
Sqlite_test::Sqlite_test(QWidget *parent) :QWidget(parent)
{
open_sqlite_pushbutton=new QPushButton("打开");
messageEdit=new QLineEdit();
QVBoxLayout *vboxlayout=new QVBoxLayout;
messageEdit2=new QLineEdit();
vboxlayout->addWidget(open_sqlite_pushbutton);
vboxlayout->addWidget(messageEdit);
vboxlayout->addWidget(messageEdit2);
setLayout(vboxlayout);
connect(open_sqlite_pushbutton,SIGNAL(clicked()),this,SLOT(open_sqlite()));
}
void Sqlite_test::open_sqlite()
{
QSqlDatabase dbconn=QSqlDatabase::addDatabase("QSQLITE");
dbconn.setDatabaseName("mytest1.db"); //在工程目录新建一个mytest.db的文件
if(!dbconn.open())
{
qDebug()<<"DB open failed!\n";
return;
}
printf("i am here ********\n");
QSqlQuery query(dbconn);//以下执行相关QSL语句
printf("i am here 222 ********\n");
query.exec("create table student(id varchar,name varchar)"); printf("i am here 333********\n");
//新建student表,id设置为主键,还有一个name项 query.exec(QObject::tr("insert into student values(1,'a')"));
query.exec(QObject::tr("insert into student values(2,'b')"));
query.exec(QObject::tr("insert into student values(3,'c')"));
query.exec(QObject::tr("insert into student values(3,'d')"));
query.exec(QObject::tr("insert into student values(4,’e')"));
query.exec(QObject::tr("insert into student values(5,'f')"));
query.exec("select id,name from student where id>=1");
printf("i am here 44444********\n");
if(!query.first())
{
qDebug()<<query.lastError().text();
}
while(query.next())//query.next()指向查找到的第一条记录,然后每次后移一条记录{
printf("i am here 555********\n");
int ele0=query.value(0).toInt();printf("i am here 666 ********\n");//query.value(0)是id的值,将其转换为int型
printf("i am here 777********\n");
qDebug()<<ele0<<ele1;printf("i am here 888********\n");//输出两个值
}
printf("i am here 9999********\n");
printf("ele0=%d\n",ele0);
}
query.exec(QObject::tr("drop student"));
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询