Skip to content

Instantly share code, notes, and snippets.

@vprus
Created September 28, 2015 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vprus/f17f515600e79814d95a to your computer and use it in GitHub Desktop.
Save vprus/f17f515600e79814d95a to your computer and use it in GitHub Desktop.
QSqlQuery + SQLite not reporting undefined named placeholders
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QTemporaryFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto database = QSqlDatabase::addDatabase("QSQLITE", "core");
QTemporaryFile databaseFile("dbXXXXXX.sqlite");
databaseFile.open();
database.setDatabaseName(databaseFile.fileName());
database.open();
qDebug() << "Opened: " << databaseFile.fileName();
QSqlQuery q1(database);
q1.prepare("create table test (id integer, data integer)");
q1.exec();
if (q1.lastError().isValid()) {
qDebug() << "Cannot create table: " << q1.lastError();
}
QSqlQuery q2(database);
q2.prepare("insert into test values (1, :data)");
q2.exec();
if (q2.lastError().isValid()) {
qDebug() << "Cannot insert data: " << q2.lastError();
} else {
qDebug() << "Inserted " << q2.numRowsAffected() << " rows ";
}
QSqlQuery q3(database);
q3.prepare("select * from test");
q3.exec();
q3.next();
qDebug() << "Obtained: " << q3.value(0) << ", " << q3.value(1);
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment