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 error : QSqlError(-1, "Driver not loaded", "Driver not loaded")