开发者

Is there a more expressive way of executing SQL query using Qt?

I currently have this code:

  // Construct query
  QString const statement = QString("drop database if exists %1")
                                    .arg(databaseName_);

  QSqlQuery query(db);
  query.exec(statement);

Is there a better way to code than the abov开发者_开发问答e?

Specifically, I dont like how I use QString for SQL statement. It'd be nice if Qt has some class so that I could do something like:

  // Construct query
  QSomeClass statement = "drop database if exists %1";
  statement.setArg(1, databaseName_); // Replace all %1 in the original string.

  QSqlQuery query(db);
  query.exec(statement);


I think you are basically describing query 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();

The only difference between the code snipped above and QSomeClass you described is the fact that you have to specify database when creating the query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜