Results 1 to 7 of 7

Thread: Need Help with QT and SQLite

  1. #1
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Need Help with QT and SQLite

    Hi,
    In my application I want to use a SQLite Database which I have it in the File . I created the connection as shown in the examples and I want display only one value from the the data base in a label on the main window.

    this is the code i have

    Qt Code:
    1. QSqlQuery qwery;
    2. qwery.exec("Select pwd from pwdtbl where Role = 1");
    3. QSqlRecord record = qwery.record();
    4. QString result = qwery.value(record.indexOf("pwd")).toString();
    5. ui->lblMWinput->text()= result;
    To copy to clipboard, switch view to plain text mode 

    if this is not the right way can please someone direct me in the right way ..

    Thanks,
    Regards.
    Last edited by chetu1984; 3rd March 2011 at 22:41.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Need Help with QT and SQLite

    From the QSqlQuery::exec () docs:
    After the query is executed, the query is positioned on an invalid record and must be navigated to a valid record before data values can be retrieved (for example, using next()).
    You should also check that the exec() call succeeded rather than discard its return value.

  3. The following user says thank you to ChrisW67 for this useful post:

    chetu1984 (8th March 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need Help with QT and SQLite

    First of all thanks ChrisW67 for the Quick response

    Coming to the Question now i am trying it this way ..

    My code is
    Connection. h
    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4.  
    5. #include <QMessageBox>
    6. #include <QtSql/QSQLiteDriver>
    7. #include <QtSql/QSqlError>
    8. #include <QtSql/QSqlQuery>
    9. #include <QDebug>
    10.  
    11.  
    12.  
    13.  
    14. static bool createConnection()
    15. {
    16. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    17.  
    18. db.setDatabaseName("./RMD0_2.db");
    19.  
    20. db.open();
    21.  
    22. qDebug()<< db <<db.isOpen();
    23.  
    24. if (!db.open()) {
    25. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    26. qApp->tr("Unable to establish a database connection.\n"
    27. "This example needs SQLite support. Please read "
    28. "the Qt SQL driver documentation for information how "
    29. "to build it.\n\n"
    30. "Click Cancel to exit."), QMessageBox::Cancel);
    31. return false;
    32. }
    33.  
    34. return true;
    35.  
    36.  
    37.  
    38. }
    39.  
    40.  
    41. #endif // CONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    And the main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. #include <connection.h>
    4. #include <QMessageBox>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. if (!createConnection()){
    11.  
    12. return 1;
    13. }
    14.  
    15. MainWindow w;
    16. w.show();
    17.  
    18. return a.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 


    The mainwindow.cpp

    Qt Code:
    1. void MainWindow::menuAction()
    2. {
    3.  
    4. QString qexe= "SELECT STAFF_ID FROM EVENT_STAFF WHERE EVENT_STAFF_ID = 2";
    5. q.prepare(qexe);
    6. if(!q.exec())
    7. qDebug()<< q.lastError();
    8. else
    9. mm.show();
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    my out put is :
    QSqlDatabase(driver=""QSQLITE"", database=""./RMD0_2.db"", host="""", port=-1, user="""", open=true) true

    and when i click on button menuAction in the mainwindow.cpp
    QSqlError(-1, "Unable to fetch row", "No query")

    what can be the problem.?

  5. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Help with QT and SQLite

    Chetu, for a test, try changing your code like this:

    Qt Code:
    1. q.exec( "SELECT STAFF_ID FROM EVENT_STAFF WHERE EVENT_STAFF_ID = 2");
    2. q.last();
    3. qDebug() << "query error is " << q.lastError(); // just to check
    4. QString staff_id = q.value(0).toString(); // get the value from select field 0
    5. qDebug() << "staff_id is now " << staff_id; // check the value
    To copy to clipboard, switch view to plain text mode 

    I think you will find this works.

    You only need to do a query.prepare then a query .exec if you have parameters you need to substitute like event_staff_id using query.addBindValue(<value>);

  6. The following user says thank you to waynew for this useful post:

    chetu1984 (8th March 2011)

  7. #5
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need Help with QT and SQLite

    Thanks for the help waynew

    when i tried to execute the code as you said the output is as follows

    Starting C:\Qt\2010.05\qt\TestRM-build-desktop\debug\TestRM.exe...
    QSqlDatabase(driver=""QSQLITE"", database=""./RMD0_2.db"", host="""", port=-1, user="""", open=true) true
    query error is QSqlError(1, "Unable to execute statement", "no such table: EVENT_STAFF")
    QSqlQuery::value: not positioned on a valid record
    staff_id is now ""
    and the port = -1 what does that signify..

  8. #6
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need Help with QT and SQLite

    I am sorry .. the problem was with the database path ..

    now everything works fine

  9. #7
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Help with QT and SQLite

    Glad you got it working ok!

Similar Threads

  1. Qt SQLite
    By metRo_ in forum Qt Programming
    Replies: 3
    Last Post: 12th December 2010, 10:35
  2. [Qt][SQLite] Two problems with SQLite.
    By Xandareva in forum Newbie
    Replies: 6
    Last Post: 6th April 2010, 23:06
  3. Help Sqlite + QT
    By vinny gracindo in forum Newbie
    Replies: 4
    Last Post: 5th December 2009, 07:33
  4. SQLite in QT
    By sophister in forum Qt Programming
    Replies: 11
    Last Post: 8th April 2009, 16:09
  5. sqlite
    By spx2 in forum Qt Programming
    Replies: 9
    Last Post: 19th December 2006, 22:01

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.