1、绑定值
QSqlQuery 支持将参数值绑定到占位符。
下面展示了使用几种不同绑定方法将值绑定到存储过程的示例。
1、使用命名占位符的命名绑定:
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();
2、使用命名占位符的位置绑定:
QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
"VALUES (:id, :forename, :surname)");
query.bindValue(0, 1001);
query.bindValue(1, "Bart");
query.bindValue(2, "Simpson");
query.exec();
3、使用位置占位符绑定值(版本 1):
QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
"VALUES (?, ?, ?)");
query.bindValue(0, 1001);
query.bindValue(1, "Bart");
query.bindValue(2, "Simpson");
query.exec();
4、使用位置占位符绑定值(版本 2):
QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
"VALUES (?, ?, ?)");
query.addBindValue(1001);
query.addBindValue("Bart");
query.addBindValue("Simpson");
query.exec();
5、将值绑定到存储过程。下面代码调用名为 AsciiToInt() 的存储过程,通过其 in 参数向其传递一个字符,并将其结果作为 out 参数。
QSqlQuery query;
query.prepare("CALL AsciiToInt(?, ?)");
query.bindValue(0, "A");
query.bindValue(1, 0, QSql::Out);
query.exec();
int i = query.boundValue(1).toInt(); // i is 65
SELECT * FROM table WHERE condition1 AND condition2;
eg:
SELECT identity,patid FROM personInfo where patid = 11202201191498 AND createtime BETWEEN '2023-09-05 0:0:0' AND '2023-09-08 23:59:59' ORDER BY createtime DESC
或者
SELECT identity,patid FROM personInfo where patid = 11202201191498 AND createtime >= '2023-09-05 0:0:0' AND createtime <= '2023-09-09 23:59:59' ORDER BY createtime DESC