Reputation: 63
I tried using sqlite in qt but I’ve come across 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 out put of the debug :
(“QSQLITE”, “QODBC3”, “QODBC”)
open: true
out of memory Unable to execute statement
insert : false
out of memory Unable to execute statement
Upvotes: 4
Views: 1911
Reputation: 9
open database before set the query(db) works for me. add this to the head.
if(!db.isOpened()) db.open();
if(db.isOpenError()) return false;
Upvotes: 0
Reputation: 121
A couple problems I see that should get this working.
1. You need to pass the database object to the QSqlQuery when you create it.
The below line is wrong
QSqlQuery createQuery;
Change it to the following and you should be good
QSqlQuery createQuery(DB);
2. You need to open the database before you create the QSqlQuery object. The connection to the database needs to be open if you initialize the QSqlQuery object with it.
So instead of this:
QSqlQuery createQuery(DB);
qDebug()<< "open: " << DB.open();
do this
qDebug()<< "open: " << DB.open();
QSqlQuery createQuery(DB);
That should get things working.
Upvotes: 2