【发布时间】:2010-09-04 06:27:34
【问题描述】:
我正在使用 QSqlQuery::prepare() 和 ::addBindValue() 在我正在处理的 Qt 项目中进行查询。有很多重复的代码,虽然我认为这是“正确”的方式,但我想确定一下。也许有人有其他想法?示例:
QSqlQuery newQuery;
newQuery.prepare("INSERT INTO table "
"(foo,bar,baz,"
"herp,derp,biggerp,"
"alpha,beta,gamma,"
"etc) VALUES "
"(?,?,?,"
"?,?,?,"
"?,?,?,"
"?)");
newQuery.addBindValue(this->ui->txtFoo->text());
newQuery.addBindValue(this->ui->txtBar->text());
newQuery.addBindValue(this->ui->txtBaz->text());
newQuery.addBindValue(this->ui->txtHerp->text());
newQuery.addBindValue(this->ui->txtDerp->text());
newQuery.addBindValue(this->ui->txtBiggerp->text());
newQuery.addBindValue(this->ui->txtAlpha->text());
newQuery.addBindValue(this->ui->txtBeta->text());
newQuery.addBindValue(this->ui->txtGamma->itemText(0));
newQuery.addBindValue(this->ui->txtEtc->text());
newQuery.exec();
您会一遍又一遍地看到一堆相同的“newQuery.addBindValue(this->ui->______”。这是“最好”的解决方法吗?
另外,前几天晚上我在 freenode 上的#qt 中问过,但没有得到明确的答案;以上(::prepare with ::addBindValue)会防止 SQL 注入吗?参考并没有真正说。
【问题讨论】:
-
首先,您不需要
this->。事实上,这可能是一种不好的做法,因为它只会让一切变得混乱。
标签: c++ qt sqlite prepared-statement