Qt连接SQLite的连接问题,连接不上?
最近有一个问题,各位老师看能不能帮下我?Qt连接Sqlite问题,连接不上?环境:WINXPQT:NokiaQTSDK里的Qtcreator数据库:SQLITE3工程目录...
最近有一个问题,各位老师看能不能帮下我?
Qt连接Sqlite问题,连接不上?
环境:WINXP QT:Nokia QT SDK里的Qt creator 数据库:SQLITE3
工程目录下,有connection.h , main.cpp, yourdb.db
在connection.h中有如下关键连接代码:
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("yourdb.db");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QSqlQuery query;
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
query.exec("insert into person values(102, 'Christine', 'Holand')");
query.exec("insert into person values(103, 'Lars', 'Gordon')");
query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
return true;
}
在main.cpp中有如下关键代码:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QSqlQuery>
#include <QSqlDatabase>
#include "connection.h"
#include <qDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
if (!createConnection())
return 1;
#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endif
return a.exec();
}
请问,编译后,是不是应该在指定的数据库中出现相应的表和数据,可以却一点反应没有。
请问,是不是只要把SQLiTE数据库放到工程目录下,并且书写如下三句话就可以连接数据库了?是不是不用注册之类的?
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("yourdb.db");
db.open()
提前感谢各位老师! 展开
Qt连接Sqlite问题,连接不上?
环境:WINXP QT:Nokia QT SDK里的Qt creator 数据库:SQLITE3
工程目录下,有connection.h , main.cpp, yourdb.db
在connection.h中有如下关键连接代码:
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("yourdb.db");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
QSqlQuery query;
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
query.exec("insert into person values(102, 'Christine', 'Holand')");
query.exec("insert into person values(103, 'Lars', 'Gordon')");
query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
return true;
}
在main.cpp中有如下关键代码:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QSqlQuery>
#include <QSqlDatabase>
#include "connection.h"
#include <qDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
if (!createConnection())
return 1;
#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endif
return a.exec();
}
请问,编译后,是不是应该在指定的数据库中出现相应的表和数据,可以却一点反应没有。
请问,是不是只要把SQLiTE数据库放到工程目录下,并且书写如下三句话就可以连接数据库了?是不是不用注册之类的?
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("yourdb.db");
db.open()
提前感谢各位老师! 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询