Results 1 to 4 of 4

Thread: SQLError -1?

  1. #1
    Join Date
    Apr 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default SQLError -1?

    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:
    Qt Code:
    1. QSqlError(-1, "", "")
    To copy to clipboard, switch view to plain text mode 

    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 )

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

    Qt Code:
    1. #include <QDebug>
    2. #include <QtSql>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    8.  
    9. // db.setHostName("localhost");
    10. // db.setDatabaseName("data.db");
    11. db.setDatabaseName(":memory:");
    12.  
    13. if (!db.open()) {
    14. qWarning("Can't open database");
    15.  
    16. } else {
    17.  
    18. QSqlQuery query;
    19. query.exec("create table person (id int primary key, "
    20. "firstname varchar(20), lastname varchar(20))");
    21. query.exec("insert into person values(101, 'Danny', 'Young')");
    22. query.exec("insert into person values(102, 'Christine', 'Holand')");
    23. query.exec("insert into person values(103, 'Lars', 'Gordon')");
    24. query.exec("insert into person values(104, 'Roberto', 'Robitaille')");
    25. query.exec("insert into person values(105, 'Maria', 'Papadopoulos')");
    26.  
    27. QSqlQuery select;
    28. select.exec("select * from person");
    29. qDebug() << select.record().value(1).toString();
    30.  
    31. qDebug() << db.lastError();
    32. qDebug() << QSqlDatabase::drivers();[/INDENT]
    33.  
    34. }
    35.  
    36. Principal w;
    37. w.show();
    38.  
    39. return a.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQLError -1?

    It means the error is invalid, thus there is no error.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: SQLError -1?

    Quote Originally Posted by wysota View Post
    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?

  4. #4
    Join Date
    Apr 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: SQLError -1?

    You were right , 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).

    Qt Code:
    1. QSqlQuery q("select * from person");
    2. QSqlRecord rec = q.record();
    3.  
    4. while (q.next())
    5. qDebug() << q.value(2).toString();
    6. // qDebug() << q.value("firstname").toString();
    To copy to clipboard, switch view to plain text mode 

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.