EasyQtSql
Easy SQL data access helper for QtSql
EasyQtSql User Manual

Introduction

EasyQtSql is a lightweight header-only library for quick and easy SQL querying with QtSql classes.

Quick Start guide

Step 1: Create Qt application project

You can use QtCreator to create a simple minimal console application with a single main.cpp file.

Or use a ready-made application where you want to add database support.

Step 2: Add QtSql support

Open *.pro file and add QtSql module (QT += sql):

QT += sql
QT -= gui
CONFIG += c++11
TARGET = App
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app

Step 3: Include EasyQtSql library

To use my library you need to include single header file (EasyQtSql.h) in your source code. So you need to put the library header files (EasyQtSql folder) in an accessible place next to your project and to specify path to the EasyQtSql folder with INCLUDEPATH directive. You can just include EasyQtSql.pri inside your *.pro file:

QT += sql
QT -= gui
include(../EasyQtSql/EasyQtSql.pri)
CONFIG += c++11
TARGET = App
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
#INCLUDEPATH += ../EasyQtSql
SOURCES += main.cpp \ TestObject.cpp
HEADERS += \
TestObject.h

Step 4: Include the library header file and start using it

Include EasyQtSql.h header file and start using the library.

#include <QCoreApplication>
#include "EasyQtSql.h"
#include "TestObject.h"
using namespace EasyQtSql;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "begin";
QSqlDatabase sdb = QSqlDatabase::addDatabase("QSQLITE");
sdb.setDatabaseName(":memory:");
try
{
Transaction t(sdb);
t.execNonQuery("CREATE TEMP TABLE IF NOT EXISTS table1 (a int, b int, c int, d text)");
//multi insert
NonQueryResult res1 = t.insertInto("table1 (a, b, c, d)")
.values(1, 2, 3, 7)
.values(2, 3, 4, "test")
.values(2, 3, 4, 8)
.exec();
qDebug() << res1.lastQuery();
//delete with condition
//t.deleteFrom("table1").where("a = ? AND b = ?", 1, 2);
//delete without condition
//t.deleteFrom("table1").exec();
//delete with constant condition
//t.deleteFrom("table1").where("1=0");
PreparedQuery query = t.prepare("SELECT a, b, c, d FROM table1 WHERE a = ? AND b = ?");
QueryResult res = query.exec(1, 2);
qDebug() << res.executedQuery() << res.boundValue(0) << res.boundValue(1);
while(res.next())
{
QVariantMap map = res.toMap();
qDebug() << map;
}
res = query.exec(2, 3);
qDebug() << res.executedQuery() << res.boundValue(0) << res.boundValue(1);
while(res.next())
{
QStringList list = res.toStringList();
qDebug() << list;
TestObject obj;
int a;
bool b;
double c;
QVariant d;
QByteArray arr;
res.fetchVars(a, b, c, d, arr);
res.fetchObject(obj);
qDebug() << a << b << c << d;
}
NonQueryResult res2 = t.update("table1")
.set("a", 123)
.set("b", 333)
.set(QVariantMap{{"a", 1}, {"b", 2}})
.where("c=? AND d=?", 3, 4);
qDebug() << res2.lastQuery() << res2.executedQuery();
t.commit();
qDebug() << "end";
}
catch (const DBException &e)
{
qDebug() << e.lastError << e.lastQuery;
}
qDebug() << "done";
return a.exec();
}

What's next?

You can:

  • Read the documentation
  • Explore and run Qt tests
  • Explore the source code of the library
  • Use the library in your project
  • Give me feedback

License

  The MIT License (MIT)
  Copyright 2018 Alexey Kramin
 
  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:
 
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
 
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.