Last active
September 29, 2020 10:37
-
-
Save jamalkhan2k6/7132660 to your computer and use it in GitHub Desktop.
QT SQlite Abstraction Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "sqlite_class.h" | |
Sqlite_Class::Sqlite_Class(QString filename) | |
{ | |
db = QSqlDatabase::addDatabase("QSQLITE"); | |
QString db_path = QDir::currentPath(); | |
qDebug() << db_path; //current path | |
db_path = db_path + filename; | |
db.setDatabaseName(db_path); | |
db.open(); | |
qDebug() << "Sqlite Connection: " <<db.lastError(); | |
QSqlQuery qry; | |
query = qry; | |
} | |
int Sqlite_Class::sql_select(QString select, QString from, QString where){ | |
QString sql = QString("SELECT ") + select + QString(" FROM ") + from + QString(" WHERE ") + where ; | |
qDebug() << sql; | |
query.exec(sql); | |
return 0; | |
} | |
int Sqlite_Class::sql_insert(QString table, QStringList data, QStringList values){ | |
QString sql = QString("INSERT INTO ") + table + QString(" ( ") ; | |
QString dt = ""; | |
QString vl = ""; | |
for(int i = 0; i < data.size(); i++){ | |
dt = dt + QString("`") + data.at(i) + QString("`") + QString(","); | |
vl = vl + QString("'") + values.at(i) + QString("'") + QString(","); | |
} | |
dt.chop(1); | |
vl.chop(1); | |
sql = sql + dt + QString(") ") + QString("VALUES(") + vl + QString(")"); | |
query.exec(sql); | |
qDebug() << db.lastError(); | |
return query.numRowsAffected(); | |
} | |
int Sqlite_Class::sql_update(QString table, QStringList data, QStringList values, QString where){ | |
QString sql = QString("UPDATE `") + table + QString("` SET ") ; | |
QString dt = ""; | |
QString vl = ""; | |
QString temp = ""; | |
for(int i = 0; i < data.size(); i++){ | |
temp = temp + QString("`") + data.at(i) + QString("`") + QString("=") + QString("'") + values.at(i) + QString("'") + QString(","); | |
} | |
temp.chop(1); | |
sql = sql + temp + QString(" WHERE ") + where; | |
qDebug() << sql; | |
query.exec(sql); | |
qDebug() << db.lastError(); | |
return query.numRowsAffected(); | |
} | |
QSqlError Sqlite_Class::sql_getLastError(){ | |
return db.lastError(); | |
} | |
int Sqlite_Class::sql_delete(QString table, QString where){ | |
QString sql = QString("DELETE ") + QString("FROM ") + table + QString(" WHERE ") + where ; | |
qDebug() << sql; | |
QSqlQuery qry; | |
qry.exec(sql); | |
query = qry; | |
return 0; | |
} | |
QSqlQuery Sqlite_Class::sql_getResults(){ | |
return query; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SQLITE_CLASS_H | |
#define SQLITE_CLASS_H | |
#include <QApplication> | |
#include <QSqlDatabase> | |
#include <QSql> | |
#include <QSqlError> | |
#include <QDir> | |
#include <QFile> | |
#include <QDebug> | |
#include <QSqlQuery> | |
#include <QString> | |
#include <QStringList> | |
#include <QSqlError> | |
class Sqlite_Class | |
{ | |
public: | |
Sqlite_Class(QString str); | |
int sql_insert(QString table, QStringList data, QStringList values); | |
int sql_select(QString select, QString from, QString where); | |
int sql_update(QString table, QStringList data, QStringList values, QString where); | |
int sql_delete(QString table, QString where); | |
QSqlQuery sql_getResults(); | |
QSqlError sql_getLastError(); | |
private: | |
QSqlQuery query; | |
QSqlDatabase db; | |
}; | |
#endif // SQLITE_CLASS_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't forget to add:
QT += sql
in your .pro file.