Take the 2-minute tour ×

if (!db.open()) {
    qDebug() << "Database open error." << db.lastError();

QSqlQuery query(db);
query.prepare("create table if not exists dew (id int, title varchar(255) not null)");
if (!query.exec()) {
    qDebug() << "Query exec error. " << query.lastError();
qDebug() << "Insert query exec OK";

if (!query.exec("insert into dew(id, title) values (1, 'hello')")) qDebug() << query.lastError();

Shows the output as

Insert query exec OK 
QSqlError(1, "Unable to execute statement", "table dew has no column named id") 
Insertion finished. 

Table creation seems to be OK. But where is id field? I'm confused with this code. I test query.record().contains("id"); and it is false

share|improve this question
If, before you run that code, a table named dew exists without an id column, that table won't be changed. Did you check? –  Mat Apr 6 '12 at 10:55
Well! You guessed right! But another thing, :temp: should suppossed to be deleted after the exec finishes. But it stays. –  Dewsworld Apr 6 '12 at 11:41
Where did you read that? –  Mat Apr 6 '12 at 11:51
The Book of Qt 4 - The Art of Building Qt Applications [Daniel Molekentin]. 'After the exec finishes' - I meant after the QApplication::exec() finished :) –  Dewsworld Apr 6 '12 at 12:06
add comment

1 Answer

up vote 1 down vote accepted

":temp:" is not a valid name for a temporary database, this creates a regular database on disk which stays after the database connection is closed.

To create a temporary or an in-memory sqlite database, that won't be saved to a file, you need to pass respectively an empty string (*) or the special string ":memory:" as the database name.


Qt doesn't allow an empty QString as the database name, but a QString starting with a '\0' should work: db.setDatabaseName(QChar(0));

share|improve this answer
add comment

Your Answer


