class DbCommand
{
friend class DbQuery;
public:
template<typename ...Args>
DbCommand(const QString& prepare_sql, Args... args) {
//qDebug() << prepare_sql;
SetPrepareSql(prepare_sql);
parse(args...);
}
DbCommand() = default;
DbCommand(const DbCommand&) = default;
QString ToString();
private:
void SetPrepareSql(const QString& sql);
void AddBindValue(const QVariant& var);
//递归终止函数
void parse(){}
//展开函数
template <typename T, typename ...Args>
void parse(T head, Args... rest)
{
AddBindValue(head);
//qDebug() << head;
parse(rest...);
}
private:
QString prepare_sql_;
QVariantList variant_list_;
};
以下是示例
DbCommand clear_table("delete from T_TEST");
auto clear_sqlquery = DbHandler().Connection("test_connection").GetQuery();
clear_sqlquery.SetCommand(clear_table);
clear_sqlquery.Exec();
DbCommand command("select id, value from T_TEST where id = ?", 123);
auto query = DbHandler().Connection("test_connection").GetQuery();
query.SetCommand(command);
if(!query.Exec())
{
qDebug().noquote() << command.ToString();
qDebug() << query.ErrorString(); //ERROR REPORT
}
auto result = query.Result();
while(result.Next())
{
qDebug() << "id\t" << "value" ;
qDebug() << result.Value("id").toInt() << "\t" << result.Value("value").toInt();