c++ SQL Query Building Library

5.8k views Asked by At

I am looking for a c++ library that provides similar functionality to the c# SelectQueryBuilder library

http://www.codeproject.com/Articles/13419/SelectQueryBuilder-Building-complex-and-flexible-S

i.e. it allows one to get away from building horrible concatenated strings into order to form a dynamic SQL query, instead having a library that provides an interface where by you pass it the table, the elements you want to select from the table, etc, and it returns the SQL query as a string.

Any help much appreciated

Edit: Sample Query I am building....and we wont know the actual columns for selection until runtime e.g. don't know how many VAR1...VARx there will be and what exactly they will be.

SELECT * FROM 
    (
        SELECT 
            table_1.id, 
            table_2.name, 
            (select(COALESCE(sum(table_1.col_1 * 1.0) / NULLIF(sum(table_1.col_2 - table_1.col_3),0) * 100,0))) as VAR1, 
            (select(COALESCE(sum(table_1.col_4 * 1.0) / NULLIF(sum(table_1.col_5),0) * 100,0))) as VAR2, 
            sum(table_1.col_2) as VAR3 
        FROM table_1, table_2 
        WHERE table_1.id = table_2.id 
        GROUP BY table_1.id, table_2.name 
    ) VARIABLES 
WHERE VAR3 > 1000
1

There are 1 answers

4
headsvk On

With QSqlQuery you can use placeholders and bind values to them:

QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
              "VALUES (:id, :forename, :surname)");
query.bindValue(":id", 1001);
query.bindValue(":forename", "Bart");
query.bindValue(":surname", "Simpson");
query.exec();

or

QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
              "VALUES (?, ?, ?)");
query.addBindValue(1001);
query.addBindValue("Bart");
query.addBindValue("Simpson");
query.exec();

http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html#approaches-to-binding-values