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":http://doc.qt.nokia.com/stable/qtsql.html module, "SQL Programming":http://doc.qt.nokia.com/stable/sql-programming.html and the "examples":http://doc.qt.nokia.com/stable/examples-sql.html, 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":http://www.catb.org/~esr/faqs/smart-questions.html.

    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

    1. 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"
    #include"QtSql/qsqldatabase.h"
    #include"QtSql/qsql.h"
    #include"QtSql/QSQLiteDriver"
    #include"QtSql/qsqlquery.h"
    db01::db01(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::db01)
    {
    ui->setupUi(this);
    connect_db();
    }

    db01::~db01()
    {
    delete ui;
    }

    void db01::connect_db()
    {
    QSqlDatabase dbs=QSqlDatabase::addDatabase("QSQLiteDriver");
    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "testdatabase.db" );
    if( !db.open() )
    {
    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.