Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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..
HuXiKa last edited by
"Qt Database Classes":http://doc.qt.nokia.com/stable/database.html
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.
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.
db01::db01(QWidget *parent) :
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
goetz last edited by
This does not work and is most likely spitting out errors:
I Removed the line of code that you mentioned and the program is now executing without any errors.
goetz last edited by
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.