欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Qt QSqlQuery 的用法

最编程 2024-03-06 07:57:38
...

重点:

1.QSqlQuery可以进行sql语句的增删改查操作。

  QSqlQuery query;    //查询某个empNo的Memo和Photo字段的数据
    query.prepare("select EmpNo, Memo, Photo from employee where EmpNo = :ID");
    query.bindValue(":ID",empNo);
    query.exec(); //其中exec表示执行sql语句。
    query.first();//定位到第一条语句,如果有多条相似

    QVariant    va=query.value("Photo");

通过输入Id,获取特定的sql数据。

2.QSqlQuery由以下的构造函数

QSqlQuery(const QsqlDatabase &db)
QSqlQuery(constQString &query=QString(, constaSqlDatabase &db =QSqlDatabase())

不传递参数,表示使用默认的数据库进行链接。

void bindValue(const QString &placeholder, const QVariant &val, QSql::ParamType paramType = QSql.ln)
void bindValue(int pos, const QVariant &val, QSql:ParamType paramType = aSql.ln)

sql的bingValue方式有两种。

其一就是:

QSqlQuery query;    //查询某个empNo的Memo和Photo字段的数据
    query.prepare("select EmpNo, Memo, Photo from employee where EmpNo = :ID");
    query.bindValue(":ID",empNo);

其二就是:

QSqlQuery query;
query.prepare("UPDATE employee SET Department=?, Salary=? WHERE EmpNo =?");

query.bindValue(0,"技术部”);
query.bindValue(1,5000);
query.bindValue(2,2006);
query.exec();