Skip to content

Instantly share code, notes, and snippets.

@jamalkhan2k6
Created October 22, 2013 08:20
Show Gist options
  • Save jamalkhan2k6/7097001 to your computer and use it in GitHub Desktop.
Save jamalkhan2k6/7097001 to your computer and use it in GitHub Desktop.
Connect to Sqlite database file using QT.
#include "mainwindow.h"
#include <QApplication>
#include <QSqlDatabase>
#include <QSql>
#include <QSqlError>
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QSqlQuery>
#include <QString>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
QString db_path = QDir::currentPath();
qDebug() <<db_path; //current path
db_path = db_path + QString("/my.db.sqlite");
db.setDatabaseName(db_path);
db.open();
qDebug() << db.lastError();
QSqlQuery query;
query.exec("SELECT * FROM attendance WHERE 1");
while(query.next()){
qDebug() << query.value(2).toString();
}
return a.exec();
}
@jamalkhan2k6
Copy link
Author

You have to add sql to your .pro file too:

QT += sql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment