Allright, revisiting this thread as I have come up with a solution to my problem, and not contributing to a closure when you have found a workaround is bad form. Basically the solution was be more competent. (even though I have no idea why my initial approach didn’t work in the first place, but I digress). I scrapped the notion of running direct queries on the Sqlite database, and switched my entire codebase over to QT’s model view architecture. So where my code previously looked like this:
if (!this->db.open()) {
qDebug() <<this->db.lastError().text();
}
query.prepare
("INSERT INTO inventory (name, description, category) VALUES (?,?,?)");
query.addBindValue(name);
query.addBindValue(description);
query.addBindValue(category);
if (!query.exec()) {
qDebug() <<query.lastError().text();
}
QSqlQuery query(this->db);
if (!this->db.open()) {
qDebug() <<this->db.lastError().text();
}
query.prepare
("INSERT INTO inventory (name, description, category) VALUES (?,?,?)");
query.addBindValue(name);
query.addBindValue(description);
query.addBindValue(category);
if (!query.exec()) {
qDebug() <<query.lastError().text();
}
To copy to clipboard, switch view to plain text mode
It now looks like this:
QHash<QString, QVariant> tableInsert;
tableInsert["name"] = ui->inputName->text();
tableInsert["description"] = ui->inputDescription->toPlainText();
tableInsert["category"] = ui->inputCategory->currentData().toInt();
if (!this->inventory->insert(tableInsert)){
qDebug() << this->inventory->getModel().lastError().text();
} else {
ui->inputName->clear();
ui->inputDescription->clear();
ui->inputName->setFocus();
}
QHash<QString, QVariant> tableInsert;
tableInsert["name"] = ui->inputName->text();
tableInsert["description"] = ui->inputDescription->toPlainText();
tableInsert["category"] = ui->inputCategory->currentData().toInt();
if (!this->inventory->insert(tableInsert)){
qDebug() << this->inventory->getModel().lastError().text();
} else {
ui->inputName->clear();
ui->inputDescription->clear();
ui->inputName->setFocus();
}
To copy to clipboard, switch view to plain text mode
The Inventory object has a QSqlTableModel attached to it, it’s “insert†method reads like this:
bool Dataset::insert(QHash<QString, QVariant> values) {
QHashIterator<QString, QVariant> i(values);
while (i.hasNext()){
i.next();
record.setValue(i.key(), i.value());
}
this->model->insertRecord(-1, record);
return this->model->submitAll();
}
bool Dataset::insert(QHash<QString, QVariant> values) {
QHashIterator<QString, QVariant> i(values);
QSqlRecord record = this->model->record();
while (i.hasNext()){
i.next();
record.setValue(i.key(), i.value());
}
this->model->insertRecord(-1, record);
return this->model->submitAll();
}
To copy to clipboard, switch view to plain text mode
(where the model object is offcourse an instance of QSqlTableModel). This way I not only get more readable code, but I manage to abstract the code into a more logical workflow. Let me know if you have any comments or suggestions for improvement
Bookmarks