无法将Qt与数据库连接(SQLite)

时间:2014-10-06 11:05:21

标签: c++ qt sqlite

pro:
QT       += core gui sql

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Game
TEMPLATE = app


SOURCES += main.cpp\
    mainwindow.cpp

HEADERS  += mainwindow.h

mainwindow.cpp:

#include "QtSql"
#include "QSqlDatabase"
//#include "QtDebug"
//#include "QFileInfo"


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{


mydb = new QSqlDatabase::addDatabase("QSQLITE"); <------- ERROR LINE
mydb->setDatabaseName("C:/sqllite2/bazadannyh.sqlite");

bool ok = mydb->open();

widget = new QWidget();

mainwindow.h: 

#include <QSqlDatabase>
class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

QString *s;


QSqlDatabase *mydb;

我在这里宣布mydb,因为否则我无法在公共广告位中访问它。可以吗?


main.cpp: 

#include "mainwindow.h"
#include <QApplication>
#include "QtGui"



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();


return a.exec();
}

我选择申报DB的地方是否正确?或者我应该改变它吗?

错误:mainwindow.cpp:19:错误:期望的类型说明符        mydb = new QSqlDatabase :: addDatabase(&#34; QSQLITE&#34;);        预期&#39;;&#39;

救救我!我该怎么办?

1 个答案:

答案 0 :(得分:2)

部首:

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

QString *s;


QSqlDatabase mydb;//not a pointer

的.cpp

mydb = QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/sqllite2/bazadannyh.sqlite");

bool ok = mydb.open();