- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将 QSqlQueryModel 与准备好的语句一起使用时遇到问题。手动执行语句时一切正常,但将其绑定(bind)到模型时 - 它不会执行。另一方面,当我使用“静态”查询(通过 QString)设置模型时 - 模型执行并加载数据。我究竟做错了什么?
我像这样准备查询:
QSqlQuery query;
query.prepare(QString("SELECT \
kontrahentid \
, trim(format ('%s %s %s', kontrahentnazwafirmy, kontrahentnazwisko, kontrahentimie) ) as nazwa \
FROM kontrahent \
WHERE %1 ilike ? \
").arg(searchBy));
query.addBindValue(searchString);
它在这样查询时有效:
if (query.exec()) {
while (query.next()) {
qDebug() << "{" << query.value(0) << ", " << query.value(1) << "}";
}
}
在 postgres 日志中我有 prepare 语句,并在之后执行它。但是当它绑定(bind)到模型时它不起作用:
QSqlQueryModel* model = new QSqlQueryModel(this);
model->setQuery(query);
//model->setQuery("SELECT kontrahentid, trim(format ('%s %s %s', kontrahentnazwafirmy, kontrahentnazwisko, kontrahentimie) ) as nazwa FROM kontrahent");
if (model->lastError().isValid()) {
qDebug() << model->lastError();
return;
}
for (int i = 0; i < model->rowCount(); ++i) {
//int id = model.record(i).value("id").toInt();
//QString name = model.record(i).value("name").toString();
qDebug() << model->record(i).value(0) << model->record(i).value(1);
}
在 postgres 日志中我只有 prepare 语句,没有执行的迹象......但是当取消注释带有“静态”查询的行时(并注释掉上面的那个)它工作得很好......
我做错了什么?在将查询对象绑定(bind)到模型之前,我必须在查询对象上运行 execute() 吗?
最佳答案
do i have to run execute() on the query object before binding it to the model?
是的。 QSqlQueryModel::setQuery(const QSqlQuery & query) 的文档说:
Note that the query must be active and must not be isForwardOnly().
要使查询处于事件状态,它必须已被执行。 QSqlQuery 的文档说:
Successfully executed SQL statements set the query's state to active so that isActive() returns true. Otherwise the query's state is set to inactive. In either case, when executing a new SQL statement, the query is positioned on an invalid record. An active query must be navigated to a valid record (so that isValid() returns true) before values can be retrieved.
关于postgresql - QSqlQueryModel 准备语句(使用 QSqlQuery)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25544806/
我正在检查我创建的应用程序的内存使用情况。它进行多次调用以从数据库 (SQLite 3) 读取和写入值。我观察到以下内容: QSqlQuery::exec() 使用一些 KB 的 RAM 来执行给定的
文档在这个主题上有点含糊: 来自 void QSqlQuery::clear() : Clears the result set and releases any resources held by
我有很多 C++11 线程在运行,它们有时都需要数据库访问。在主要我做初始化数据库连接并打开数据库。 Qt 文档说查询不是线程安全的,所以我使用全局互斥锁,直到 QSqlQuery 存在于线程中。 这
我正在尝试从数据库中检索用户,他们的名字是 'DOMAIN\name'。 我已经在 sql 控制台中检查了查询,简单的选择如下: select * from users where name='DOM
SHA256 sha256; std::string s = sha256(pass.toStdString()); QString myquery = "declare @identifier nv
我正在尝试将一个字符插入到表格的列中。而是插入字符的十进制代码。如何插入字符? 详细信息: QString insertSql; insertSql .append("INSERT INTO
QMake 版本 3.0 Qt 版本 5.0.2 使用时 QSqlQuery query(QString("SELECT device_text_id FROM device")); qDebug()
我尝试用 Qt 创建一个 SQLite 数据库,我做到了!!我将我的数据库命名为“prova_db”,它包含下表: marker_db id site (columns) 0 www
我有自己的数据存储机制来存储数据。但我想为最终用户实现标准的数据操作和查询接口(interface),所以我认为 QT sql 适合我的情况。但我仍然不明白如何将索引用于 sql 查询。比方说,我有包
如何取消长时间运行的QSqlQuery? 数据库正在返回超过 300 万行,并且显示在 QTableView 控件中。我希望能够强制停止这两个长时间操作: 当数据库正在运行一个长时间的操作时 如果数据
正在关注 QSqlQuery Class documentation可以在 MySQL 数据库中实现插入操作,即像这样: QSqlQuery query; query.prepare("INSERT
当运行下面的代码时,Windows 中的结果是 'אבגדה' 和 '??????'在Linux上,似乎qry.value(0)(一种 QVarient 类型)使用默认编码。在 Linux 中,我在驱
我使用 PyQt4.QtSql.QSqlQuery。我从表单中获取值并想将它们插入到数据库中。我打开一个连接并尝试添加,但数据库中没有值,也没有错误。有什么问题? db = QSql
我正在尝试选择我的日期库的一个字段,代码是: if (db.db().isOpen()) { qDebug() << "OK"; QSqlQuery query("SELECT state F
出于测试目的,我试图在实际运行之前从 QSqlQuery 对象中获取准备好的语句字符串。 我检查了 lastQuery() 和 executedQuery() 方法,但它们都不起作用。 void fo
我最近才发现 gmock,现在正在“重新思考整个编程过程”,尽可能地添加单元测试。在此过程中让我感到奇怪的一件事是 QSql 模块显然是我们代码的外部依赖项,并没有为开发人员提供模拟其内部的工具。对于
我正在尝试使用此代码使用 qt 进行删除: int jobId = 655; query.prepare("DELETE FROM jobs WHERE jobId = '" + QString::n
我在 Qt 的 sqlite 数据库中看到莫名其妙的缓慢行为。下面代码中的查询,当从同一数据库上的 sqlite 命令行实用程序执行时,会立即完成。 qDebug() textFormIds; if
我想实现一种方法,该方法将在退出程序时检查搜索具有 localhost ip 地址的条目并删除这些行。 问题是,我不确定如何在 Qt 中执行此操作,当我搜索时,我真的找不到任何具体的东西。有没有办法在
我正在使用带有 Qt 提供程序的 firebird 嵌入式数据库。为了使用数据库,我使用 qsqlquery::exec() 函数。这个函数是同步的还是异步的? 最佳答案 QSqlQuery::exe
我是一名优秀的程序员,十分优秀!