QT Sqlite ARM 编写程序是本文要介绍的内容,主要是代码,先来看内容。
- #include<QApplication>@H_404_19@
- #include<QTextEdit>@H_404_19@
- #include<QString>@H_404_19@
- #include<qsqldatabase.h>@H_404_19@
- #include<QsqlQuery>@H_404_19@
- #include<QsqlError>@H_404_19@
- #include<QsqlDriver>@H_404_19@
- #include<QDateTime>@H_404_19@
- #include<QDebug>@H_404_19@
- intmain(intargc,char*argv[])
- {
- QApplicationapp(argc,argv);
- QTextEditdisplay;
- display.resize(300,160);
- display.show();//下面进行数据库的设置
- //QsqlDatabase*db=QsqlDatabase@H_404_19@::addDatabase("QsqlITE");//使用sqlite数据库驱动
- QsqlDatabasedb=QsqlDatabase::addDatabase("QsqlITE"); @H_404_19@
- //db.addDatabase("QsqlITE");
- db.setDatabaseName("test");//我们之前建立的数据库
- boolok=db.open();//尝试连接数据库 @H_404_19@
- if(ok)
- {//这里用text已经成功连上数据库
- QsqlQueryquery;//新建一个查询的实例
- if(query.exec("select*fromstudent"))//尝试列出student表的所有记录
- {//本次查询成功
- intnumRows=0;//询问数据库驱动,是否驱动含有某种特性 @H_404_19@
- if(db.driver()->hasFeature(QsqlDriver::QuerySize)) @H_404_19@
- {
- numRows=query.size();//如果支持结果影响的行数,那么直接记录下来 @H_404_19@
- }
- else
- {
- query.last();//否则定位到结果最后,qt文档说,这个方法非常慢
- numRows=query.at()+1; @H_404_19@
- query.seek(-1);
- }
- QStringname,age;
- display.append("===========================================");
- while(query.next())
- {//定位结果到下一条记录
- name=query.value(0).toString(); @H_404_19@
- age=query.value(1).toString(); @H_404_19@
- QStringresult=name+""+age; @H_404_19@
- display.append(result);
- }
- display.append("===========================================");
- display.append(QString("totally%1rows").arg(numRows));
- }
- else
- {//打开数据库失败,显示数据库返回的失败描述
- display.append("cannotopendatabase.");
- display.append("Reason:"+db.lastError().databaseText());
- }
- QApplication::connect(&app,SIGNAL(lastWindowClose()),&app,SLOT(quit()));
- returnapp.exec();
- }
编译出错:
- undefinedreferenceto`QsqlDatabase::defaultConnection’
解决办法:
第一种办法:
- .pro里面加一句
- QT+=sql
第二种办法:
小结:关于详解QT sqlite ARM 编写程序实例的内容介绍完了,希望本文对你会有所帮助!
【内容整理:蚂蚁爬 URL:www.z8soft.com】@H_404_19@
QT Sqlite ARM 编写程序是本文要介绍的内容,主要是代码,先来看内容。
- #include<QApplication>@H_404_19@
- #include<QTextEdit>@H_404_19@
- #include<QString>@H_404_19@
- #include<qsqldatabase.h>@H_404_19@
- #include<QsqlQuery>@H_404_19@
- #include<QsqlError>@H_404_19@
- #include<QsqlDriver>@H_404_19@
- #include<QDateTime>@H_404_19@
- #include<QDebug>@H_404_19@
- intmain(intargc,SLOT(quit()));
- returnapp.exec();
- }
编译出错:
- undefinedreferenceto`QsqlDatabase::defaultConnection’
解决办法:
第一种办法:
- .pro里面加一句
- QT+=sql
第二种办法: