4

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I forgot to ask if it possible nest the query / values and also add binds dynamically? i.e if I wanted a "select [ ] from table_1", where [] will be allocated at run time and is a list of values I would like to select?
The value binding is done at runtime, I think you could use addBindValue in a for loop. If you posted some code I could help you further.
I have added the sort of sample query I am working with (although the real examples select a lot longer list from the DB). I currently create them by concatenating strings together, but obviously I would prefer to be able to use the binding method you have suggested.
Oh, I see, you need to not only bind values but also create variable queries. I haven't seen something like that in Qt. I checked the docs qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html, but there's only the value binding approach, nothing about preparing long statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.