Important: Please read the Qt Code of Conduct -

Database connectivity in qt

  • I am a beginner , can anybody please help me out with database connectivity using qt? any basic application will do,like performing
    some operations on two numbers using qt app and storing result in database the preferred database is MS Access .
    looking forward for your reply..

  • Well, I assume you have already read the documentation about the "QtSql": module, "SQL Programming": and the "examples":, haven't you?

  • Thank you very much for your response ,i have gone through the examples,i am trying to write a basic program to connect to MS Access database but i am not able to create a instance of QSqlQuery in order to fire a 'Select' query on the database. (i tried including header file 'QSqlQuery')

  • So - what was the error?

    Have you added QT += sql to your .pro file?

  • yes i have already added that.Another doubt that i have is where should the database reside so that the qt app should make use of it?

  • Did it work after you've added it?

    Don't let us extract every single piece of information out of you. Provide clear, concrete, complete and meaningful "questions":

    As to your last question: it depends. If the data is application-local feel free to distribute the database along with your application (however I would prefer SQLite over Access in all respects). If it is shared among multiple instances or sites you may need a central database server. However, this is just a rule of thumb!

    But - again - there is no way we can give you a reasonable answer with this little amount of information you provide.

  • I apologies for not being clear about the doubts I have. I really Appreciate your immediate response to my qurey, Now I'm able to create QSqlQuery Instance.

  • hii,
    i have been trying to develop an app to connect to database using "Sqlite" i have tried just to establish connection with database.
    Program its throwing many errors i'm listing them out it would be great if you have can guide me through this.

    1)error: QtSql/private/qsqlcachedresult_p.h: No such file or directory
    2) error: 'QSqlCachedResult' has not been declared

  • Following is the class file where i have written the function "connect_db()" to establish the connection.

    #include "db01.h"
    #include "ui_db01.h"
    db01::db01(QWidget *parent) :
    ui(new Ui::db01)

    delete ui;

    void db01::connect_db()
    QSqlDatabase dbs=QSqlDatabase::addDatabase("QSQLiteDriver");
    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "testdatabase.db" );
    if( ! )
    qDebug() << db.lastError();
    qFatal( "Failed to connect." );

    Edit: Enclosed code section in @ tags. Please do that yourself next time; Andre

  • This does not work and is most likely spitting out errors:

    QSqlDatabase dbs=QSqlDatabase::addDatabase("QSQLiteDriver");

  • Hii volker,
    I Removed the line of code that you mentioned and the program is now executing without any errors.
    thanks alot

  • Good to know it works now.

    You should be aware, that the first argument to addDatabase is always the driver type (i.e. on of "QSQLITE" or "QMYSQL" for example). If you want to give your database connection a "handle name", add it as a second argument. But IMHO that's only needed in case you have to deal with multiple connections. If you only have one connection, the default scheme should be sufficient.

Log in to reply