开发环境:
Qt Creator 2.8.1
Based on Qt 5.0.2 (GCC 4.8.2,32 bit)
问题1:编译无错误,在执行时终端出现以下字母:
QsqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection',old connection removed.
问题所在代码段:
QsqlDatabase db = QsqlDatabase::addDatabase("QsqlITE");
解决方法:
QsqlDatabase db; if(QsqlDatabase::contains("GBond")) db = QsqlDatabase::database("GBond"); else db = QsqlDatabase::addDatabase("QsqlITE","GBond");
这样一来,以上问题就解决了。
不过,再次执行时终端出现以下字母:
QsqlQuery::exec: database not open QsqlQuery::exec: database not open QsqlQuery::exec: database not open QsqlQuery::exec: database not open
问题所在代码段:
QsqlQuery query; QString sql = "INSERT INTO contact VALUES(1,'smk',17,'friend','18998416978' );" ; query.exec(sql);
解决方法:
QsqlQuery query(db); QString sql = "INSERT INTO contact VALUES(1,'18998416978' );" ; query.exec(sql);
至始,该问题完美解决。
原文链接:https://www.f2er.com/sqlite/200738.html