Results 1 to 3 of 3

Thread: Problem Opening Sqlite database from Qt

  1. #1
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Problem Opening Sqlite database from Qt

    I am facing problem in openning sqlite database from Qt using QSqlDatabase. The db file and relevant tables are already created.
    Single row is also inserted.

    What can be the issue ? Tried configuring Qt Source with option "-qt-sql-sqlite" as given in help but no luck

    Output after running application is:


    Error!!!....openning db

    LastError= ""
    Rows affected = 0


    Sample Code:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QtSql>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setDatabaseName("/home/data/db/sample.db");
    10.  
    11. if(!db.open())
    12. {
    13. qDebug() << "Database successfully opened...!!! go ahead !!!" << endl;
    14. }
    15. else
    16. {
    17. qDebug() << "Error!!!....openning db" << endl;
    18. qDebug() << "LastError= " << db.lastError().databaseText();
    19. }
    20.  
    21. QSqlQuery query;
    22. query.prepare("SELECT * FROM USER_INFO");
    23. query.exec();
    24.  
    25. qDebug() << "Rows affected = " << query.numRowsAffected() << endl;
    26.  
    27. db.close();
    28. app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Project Pro file:

    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2012-10-14T19:20:29
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core
    8.  
    9. QT -= gui
    10. QT += sql
    11.  
    12. TARGET = DBTest
    13. CONFIG += console
    14. CONFIG -= app_bundle
    15.  
    16. TEMPLATE = app
    17.  
    18. SOURCES += main.cpp
    19.  
    20. unix:!macx:!symbian: LIBS += -L$$PWD/../../../../opt/QtSDK/QtCreator/lib/qtcreator/plugins/sqldrivers/ -lqsqlite
    21.  
    22. INCLUDEPATH += $$PWD/../../../../opt/QtSDK/Desktop/Qt/4.8.1/gcc/include/Qt
    23. DEPENDPATH += $$PWD/../../../../opt/QtSDK/Desktop/Qt/4.8.1/gcc/include/Qt
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2012
    Location
    Iran, Tehran
    Posts
    76
    Thanks
    17
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Problem Opening Sqlite database from Qt

    correct your condition it must be
    Qt Code:
    1. if(db.open())
    To copy to clipboard, switch view to plain text mode 


    Added after 59 minutes:


    Also take look at QSqlQuery::numRowsAffectedyou will find
    Note that for SELECT statements, the value is undefined; use size() instead.
    Last edited by Ashkan_s; 21st October 2012 at 17:18.

  3. #3
    Join Date
    Apr 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem Opening Sqlite database from Qt

    This is has been solved. !!!

Similar Threads

  1. SQLite database problem in some SO
    By anoraxis in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2012, 23:54
  2. Replies: 0
    Last Post: 21st February 2012, 16:20
  3. sqlite database problem
    By palinko1111 in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2011, 07:04
  4. Replies: 0
    Last Post: 27th February 2011, 14:39
  5. Load a new Sqlite database problem
    By beware in forum Qt Programming
    Replies: 15
    Last Post: 24th January 2011, 09:05

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.