QSqlQuery绑定查询

来源: ChinaUnix博客  日期: 2009.05.27 08:40 (共有条评论) 我要评论
 
Approaches to Binding Values
Below we present the same example using each of the four different binding approaches, as well as one example of binding values to a stored procedure.
Named binding using named placeholders:
     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();
Positional binding using named placeholders:
     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();
Binding values using positional placeholders (version 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();
Binding values using positional placeholders (version 2):
     QSqlQuery query;
     query.prepare("INSERT INTO person (id, forename, surname) "
                   "VALUES (?, ?, ?)");
     query.addBindValue(1001);
     query.addBindValue("Bart");
     query.addBindValue("Simpson");
     query.exec();
Binding values to a stored procedure:
This code calls a stored procedure called AsciiToInt(), passing it a character through its in parameter, and taking its result in the out parameter.
     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
Note that unbound parameters will retain their values.
More information to see QT Help.
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/86708/showart_1945836.html
  发表评论 查看评论(共有条评论)
 
posted on 2013-01-09 16:11 姚先进 阅读(315) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: