SQLITE from memory Failed to execute statement

I tried using sqlite in qt, but ive met an error.

qDebug() << QSqlDatabase::drivers();
QSqlDatabase DB = QSqlDatabase::addDatabase("QSQLITE");
DB.setDatabaseName("/Volumes/MAJID/majid/Naminic/db0.db");
QSqlQuery createQuery;
qDebug()<< "open: " << DB.open();
createQuery.exec("CREATE TABLE contact(name,tell)");
qDebug() << createQuery.lastError().text();

qDebug() << "insert : " << createQuery.exec("insert into contact(name,tell) values('a','b')");
qDebug() << createQuery.lastError().text();

and this is the result of debugging:

("QSQLITE", "QODBC3", "QODBC")

open: true

out of memory Unable to execute statement

insert : false

out of memory Unable to execute statement

+4
source share
1 answer

A couple of issues that I see should make this work.
1. You need to pass the database object to QSqlQuery when you create it.
Invalid line below

QSqlQuery createQuery;

Change it to the following and you should be good

QSqlQuery createQuery(DB);


2. Before creating the QSqlQuery object, you must open the database. The database connection must be open if you initialize the QSqlQuery object.

So instead:

QSqlQuery createQuery(DB);
qDebug()<< "open: " << DB.open();

do it

qDebug()<< "open: " << DB.open();
QSqlQuery createQuery(DB);

This should make everything work.

+2
source

Source: https://habr.com/ru/post/1527080/


All Articles