C ++ SQL查询构建库

C ++ SQL查询构建库

问题描述:

我正在寻找一个c ++库,该库提供与c#SelectQueryBuilder库相似的功能

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

即它允许人们摆脱构建可怕的串联字符串以形成动态SQL查询的麻烦,而拥有一个提供接口的库,您可以通过该接口将表,要从表中选择的元素传递给它,等等.返回SQL查询作为字符串.

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.

非常感谢任何帮助

示例查询我正在构建....直到运行时,我们才知道实际选择的列,例如不知道会有多少VAR1 ... VARx,以及它们到底将是什么.

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

使用QSqlQuery,您可以使用占位符并将值绑定到它们:

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();

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