Thứ Tư, 18 tháng 4, 2012

[Qt] Truyền giá trị cho các tham số trong câu lệnh sqlite

Một số cách để truyền giá trị cho tham số trong câu truy vấn SQLite trong QT

Gán giá trị theo tên tham số
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();

Gán giá trị theo thứ tự tham số

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();

Gán giá trị cho trường hợp sử dụng truyền tham số theo thứ tự(version1)

query.prepare("INSERT INTO person (id, forename, surname) "
"VALUES (?, ?, ?)");
query.bindValue(0, 1001);
query.bindValue(1, "Bart");
query.bindValue(2, "Simpson");
query.exec();
Gán giá trị cho trường hợp sử dụng truyền tham số theo thứ tự(version 2):
Sử dụng AddBinValue(giá trị) thay vì bindValue. Nhưng lưu ý là thứ thự các câu lệnh AddBinhValue phải theo đúng thứ tự tham số muốn truyền vào

query.prepare("INSERT INTO person (id, forename, surname) "
"VALUES (?, ?, ?)");
query.addBindValue(1001);
query.addBindValue("Bart");
query.addBindValue("Simpson");
query.exec();

Không có nhận xét nào:

Đăng nhận xét