PDA

View Full Version : SQLError -1?



Januz
28th June 2009, 23:31
Hi everyone!

I'm trying to connect to a qsqlite database, so far I get an open connection (at least open() returns true), but when i check lastError() I get this:

QSqlError(-1, "", "")

Queries return empty too. I've been googling my eyes out and can't find a solution (I don't even know what the problem is :p )

I'm using OS X 10.5.7 , QT 4.5.2 and QTCreator 1.2.
Here's the code:



#include <QDebug>
#include <QtSql>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

// db.setHostName("localhost");
// db.setDatabaseName("data.db");
db.setDatabaseName(":memory:");

if (!db.open()) {
qWarning("Can't open database");

} else {

QSqlQuery query;
query.exec("create table person (id int primary key, "
"firstname varchar(20), lastname varchar(20))");
query.exec("insert into person values(101, 'Danny', 'Young')");
query.exec("insert into person values(102, 'Christine', 'Holand')");
query.exec("insert into person values(103, 'Lars', 'Gordon')");
query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");

QSqlQuery select;
select.exec("select * from person");
qDebug() << select.record().value(1).toString();

qDebug() << db.lastError();
qDebug() << QSqlDatabase::drivers();[/INDENT]

}

Principal w;
w.show();

return a.exec();
}


Thanks!

wysota
28th June 2009, 23:57
It means the error is invalid, thus there is no error.

Januz
29th June 2009, 00:32
It means the error is invalid, thus there is no error.

I thought 0 meant no error, any idea why I don't get anything from my queries?

Januz
29th June 2009, 01:35
You were right :p, there was no error. I was using record() wrong.

Solution:
You need to set an index in QSqlRecord to get the values from it (q.next does that in the code below).



QSqlQuery q("select * from person");
QSqlRecord rec = q.record();

while (q.next())
qDebug() << q.value(2).toString();
// qDebug() << q.value("firstname").toString();