I currently have a release version of my Qt desktop app. I have used Sqlite database in the application. How can i give the database file with the app? so that when the application is used then the database can be accessed and used?
Asked
Active
Viewed 435 times
0
-
If you have GUI, you can use a dialog, or you can provide the database name as a command line argument to your application. – vahancho Oct 30 '13 at 12:47
-
Take a look at this posts accepted answer: http://stackoverflow.com/questions/4254250/embedded-database-in-qt – UldisK Oct 30 '13 at 13:04
-
ReadWrite, or read-only? – Frank Osterfeld Oct 30 '13 at 19:35
1 Answers
0
If you mean by this, how one can open /create or access database in qt, then you should use QSqlDatabase class, here is an example though:
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
QSqlDatabase db;
QSqlQuery query;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("db.sqlite"); // path to your database
qDebug()<<"database is opened - "<<db.open();
// this is how you can execute queries to database
query.exec("create table options "
"(id integer primary key, "
"option text)");
query.exec(QString("insert into options values(NULL,'%1')").arg(QString("look")));
query.exec(QString("insert into options values(NULL,'%1')").arg(QString("dance")));
query.exec(QString("insert into options values(NULL,'%1')").arg(QString("read")));
db.close();
and in your .pro file don't forget to add QT += sql
line

Shf
- 3,463
- 2
- 26
- 42