PDA

View Full Version : QSql - "Error opening database", "unable to open database file"



SykeS
19th May 2010, 18:24
I have the following code:


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase m = QSqlDatabase::addDatabase("QSQLITE");
m.setDatabaseName(":baza:");
bool ok = m.open();
if (ok)
{
qDebug() << "connected" << endl;
}
else
{
qDebug() << "not Connected " << endl;
qDebug() << QSqlDatabase::drivers() << endl;
qDebug() << m.lastError();
}

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}


and it's returning:


not Connected

("QSQLITE", "QODBC3", "QODBC")

QSqlError(-1, "Error opening database", "unable to open database file")

What's wrong?

tbscope
19th May 2010, 18:27
Colons are forbidden in filenames

SykeS
19th May 2010, 18:38
Hmmm.... It works, but... How to define temporary in-memory database? In QT Examples there is something like this:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
And it works, but not in my program :)

tbscope
19th May 2010, 19:12
What happens if you leave the database name empty. That should be the same as :memory: according to the SQLite docs.

Anyway: the ":memory:" string is special. I don't know how Qt handles that, if at all.
In any normal case, the databasename for a SQLite database is also the filename. And you can not have a colon in a filename.

JD2000
19th May 2010, 19:16
Not something like, but exactly like - to quote from the docs
SQLite also supports in-memory databases, simply pass ":memory:" as the database name.

SykeS
19th May 2010, 19:36
Thanks a lot!

But now I have the following code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include <QDebug>
#include <QMessageBox>
#include <QSqlTableModel>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initDatabase();
QSqlTableModel model;

model.setTable("person");
model.select();

model.setHeaderData(1, Qt::Horizontal, QObject::tr("First name"));
model.setHeaderData(2, Qt::Horizontal, QObject::tr("Last name"));
model.setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
ui->tableView->setModel(&model);
qDebug() << model.lastError();
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

bool MainWindow::initDatabase()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");

if(!db.open()){
QMessageBox::critical(this, tr("Błąd bazy danych"), tr("Nie można połączyć się z bazą danych."
"\n%1").arg("test"));
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;
}


and it prints window like in attachment. Why?

JD2000
20th May 2010, 09:10
You need
ui->tableView->show(); at the end of the MainWindow constructor to actually see the tableview.

ChrisW67
20th May 2010, 09:33
"model" goes out of scope at the end of the constructor leaving nothing for the table view to show.

SykeS
20th May 2010, 09:40
@JD2000 - I use Qt Creator.


"model" goes out of scope at the end of the constructor leaving nothing for the table view to show.
What is the solution?

JD2000
20th May 2010, 09:55
Add this to mainwidow.h

private:
QSqlTableModel *model;