0
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;

I declared mydb here, because otherwise i can't get access to it in my public slots. Is it OK?


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();
}

Is the place i choose to declare DB is correct? Or should i change it?

ERROR: mainwindow.cpp:19: error: expected type-specifier mydb = new QSqlDatabase::addDatabase("QSQLITE"); expected ';'

Help me! What should i do?

1 Answer 1

2

Header:

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();
Sign up to request clarification or add additional context in comments.

2 Comments

mainwindow.cpp:20: error: cannot convert 'QSqlDatabase' to 'QSqlDatabase*' in assignment mydb = QSqlDatabase::addDatabase("QSQLITE");
@MedetKoilybay see my edit please, it should be clear how to solve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.