qsqlquery qt 返回结果_QT数据库QSqlQuery

SQL执行操作

QSqlQuery提供了对数据库记录的Select、Insert、Update、Delete操作。

SELECT操作:

QSqlQuery query;

query.exec("SELECT name, salary FROM employee WHERE salary > 50000");

while (query.next()) {

QString name = query.value(0).toString();

int salary = query.value(1).toInt();

qDebug() << name << salary;

}

通过QSqlQuery::next()、QSqlQuery::previous()、QSqlQuery::first()、QSqlQuery::last()、QSqlQuery::seek(),可以得到下一条、上一条、第一条、最后一条、任意一条记录的位置。

INSERT操作:

//单一插入数据

QSqlQuery query;

query.prepare("INSERT INTO employee (id, name, salary) "

"VALUES (:id, :name, :salary)");

query.bindValue(":id", 1001);

query.bindValue(":name", "Thad Beaumont");

query.bindValue(":salary", 65000);

query.exec();

//批量插入数据

QSqlQuery query;

query.prepare("insert into myTable values (?, ?)");

QVariantList ints;

ints << 1 << 2 << 3 << 4;

query.addBindValue(ints);

QVariantList names;

names << "Harald" << "Boris" << "Trond" << QVariant(QVariant::String);

query.addBindValue(names);

if (!query.execBatch())

qDebug() << query.lastError();

UPDATE操作:

QSqlQuery query;

query.prepare("UPDATE employee SET salary = ? WHERE id = 1003");

query.bindValue(0, 70000);

query.exe();

DELETE操作:

QSqlQuery query;

query.exec("DELETE FROM employee WHERE id = 1007");

事务处理:

<

你可能感兴趣的:(qsqlquery,qt,返回结果)