Created
September 28, 2015 08:23
-
-
Save vprus/f17f515600e79814d95a to your computer and use it in GitHub Desktop.
QSqlQuery + SQLite not reporting undefined named placeholders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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