ld过程中报错:undefined reference to `_imp___ZN12QSqlDatabase17defaultConnectionE'
网上查了下,在
pro文件中加了Qt +=
sql,但是没用,仍然报相同的
错误 以为是sqlite驱动不对,但是查看了我的C:\QtSDK\Desktop\Qt\4.7.4\mingw\plugins\sqldrivers目录,里面已经包含了
libqsqlite4.a、libqsqlited4.a、qsqlite4.dll、qsqlited4.dll,应该是有驱动的。网上说qt默认包含sqlite驱动的
源码:
- #include <QtCore/QCoreApplication>
- #include <QtSql/QSqlDatabase>
- #include <QtSql/QSqlQuery>
- #include <QTextCodec>
- #include <QVariant>
- #include <QDebug>
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
- QSqlDatabase dbconn=QSqlDatabase::addDatabase("QSQLITE"); //添加数据库驱动
- dbconn.setDatabaseName("mytest.db"); //在工程目录新建一个mytest.db的文件
- if(!dbconn.open()) { qDebug()<<"fdsfds"; }
- QSqlQuery query;//以下执行相关QSL语句
- query.exec("create table student(id varchar,name varchar)"); //新建student表,id设置为主键,还有一个name项
- query.exec(QObject::tr("insert into student values(1,'李刚')"));
- query.exec(QObject::tr("insert into student values(2,'苹果')"));
- query.exec(QObject::tr("insert into student values(3,'葡萄')"));
- query.exec(QObject::tr("insert into student values(3,'李子')"));
- query.exec(QObject::tr("insert into student values(4,’橘子')"));
- query.exec(QObject::tr("insert into student values(5,'核桃')"));
- query.exec(QObject::tr("insert into student values(6,'芒果')"));
- //query.exec(QObject::tr("select id,name from student where id>=1"));
- query.exec("select id,name from student where id>=1");
- while(query.next())//query.next()指向查找到的第一条记录,然后每次后移一条记录
- {
- int ele0=query.value(0).toInt();//query.value(0)是id的值,将其转换为int型
- QString ele1=query.value(1).toString();
- qDebug()<<ele0<<ele1;//输出两个值
- }
- query.exec(QObject::tr("drop student"));
- return a.exec();
- }