0

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?

Mangya
  • 7
  • 1
  • 1
  • 4

1 Answers1

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