Question
bool MainWindow::initDb() {
db = QSqlDatabase::addDatabase("QSQLITE"); // defined on class header as QSqldatabase db
db.setDatabaseName("data.db");
if (!db.open()) {
QMessageBox::critical(this,
tr("Error"),
tr("Could not save data. Database problem."));
return false;
}
query.prepare("create table if not exists snippet (id int primary key, title varchar(255) not null, tags varchar(255), snippet text)");
if (!query.exec()) { // error
QMessageBox::critical(this,
tr("Database error"),
tr("Could not setup database"));
qDebug() << "Database error : " << query.lastError();
return false;
}
query.clear();
return true;
}
Database It shows Database error message box and the following in the console.
Database error : QSqlError(-1, "Driver not loaded", "Driver not loaded")
La solution
I got a partial solution. Making query as local object with passing db to it's constructor solve the problem. Like
QSqlQuery query(db);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow