📄 qsqlquery.cpp
字号:
if (isForwardOnly() && actualIdx < at()) { qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); return false; } if (actualIdx == (at() + 1) && at() != QSql::BeforeFirstRow) { if (!d->sqlResult->fetchNext()) { d->sqlResult->setAt(QSql::AfterLastRow); return false; } return true; } if (actualIdx == (at() - 1)) { if (!d->sqlResult->fetchPrevious()) { d->sqlResult->setAt(QSql::BeforeFirstRow); return false; } return true; } if (!d->sqlResult->fetch(actualIdx)) { d->sqlResult->setAt(QSql::AfterLastRow); return false; } return true;}/*! Retrieves the next record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and isSelect() must return true before calling this function or it will do nothing and return false. The following rules apply: \list \o If the result is currently located before the first record, e.g. immediately after a query is executed, an attempt is made to retrieve the first record. \o If the result is currently located after the last record, there is no change and false is returned. \o If the result is located somewhere in the middle, an attempt is made to retrieve the next record. \endlist If the record could not be retrieved, the result is positioned after the last record and false is returned. If the record is successfully retrieved, true is returned. \sa previous() first() last() seek() at() isActive() isValid()*/bool QSqlQuery::next(){ if (!isSelect() || !isActive()) return false; bool b = false; switch (at()) { case QSql::BeforeFirstRow: b = d->sqlResult->fetchFirst(); return b; case QSql::AfterLastRow: return false; default: if (!d->sqlResult->fetchNext()) { d->sqlResult->setAt(QSql::AfterLastRow); return false; } return true; }}/*! Retrieves the previous record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and isSelect() must return true before calling this function or it will do nothing and return false. The following rules apply: \list \o If the result is currently located before the first record, there is no change and false is returned. \o If the result is currently located after the last record, an attempt is made to retrieve the last record. \o If the result is somewhere in the middle, an attempt is made to retrieve the previous record. \endlist If the record could not be retrieved, the result is positioned before the first record and false is returned. If the record is successfully retrieved, true is returned. \sa next() first() last() seek() at() isActive() isValid()*/bool QSqlQuery::previous(){ if (!isSelect() || !isActive()) return false; if (isForwardOnly()) { qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); return false; } bool b = false; switch (at()) { case QSql::BeforeFirstRow: return false; case QSql::AfterLastRow: b = d->sqlResult->fetchLast(); return b; default: if (!d->sqlResult->fetchPrevious()) { d->sqlResult->setAt(QSql::BeforeFirstRow); return false; } return true; }}/*! Retrieves the first record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and isSelect() must return true before calling this function or it will do nothing and return false. Returns true if successful. If unsuccessful the query position is set to an invalid position and false is returned. \sa next() previous() last() seek() at() isActive() isValid()*/bool QSqlQuery::first(){ if (!isSelect() || !isActive()) return false; if (isForwardOnly() && at() > QSql::BeforeFirstRow) { qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); return false; } bool b = false; b = d->sqlResult->fetchFirst(); return b;}/*! Retrieves the last record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and isSelect() must return true before calling this function or it will do nothing and return false. Returns true if successful. If unsuccessful the query position is set to an invalid position and false is returned. \sa next() previous() first() seek() at() isActive() isValid()*/bool QSqlQuery::last(){ if (!isSelect() || !isActive()) return false; bool b = false; b = d->sqlResult->fetchLast(); return b;}/*! Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-\c SELECT statements (isSelect() returns false), size() will return -1. If the query is not active (isActive() returns false), -1 is returned. To determine the number of rows affected by a non-\c SELECT statement, use numRowsAffected(). \sa isActive() numRowsAffected() QSqlDriver::hasFeature()*/int QSqlQuery::size() const{ if (isActive() && d->sqlResult->driver()->hasFeature(QSqlDriver::QuerySize)) return d->sqlResult->size(); return -1;}/*! Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. Note that for \c SELECT statements, the value is undefined; use size() instead. If the query is not active (isActive() returns false), -1 is returned. \sa size() QSqlDriver::hasFeature()*/int QSqlQuery::numRowsAffected() const{ if (isActive()) return d->sqlResult->numRowsAffected(); return -1;}/*! Returns error information about the last error (if any) that occurred with this query. \sa QSqlError, QSqlDatabase::lastError()*/QSqlError QSqlQuery::lastError() const{ return d->sqlResult->lastError();}/*! Returns true if the query is currently positioned on a valid record; otherwise returns false.*/bool QSqlQuery::isValid() const{ return d->sqlResult->isValid();}/*! Returns true if the query is currently active; otherwise returns false.*/bool QSqlQuery::isActive() const{ return d->sqlResult->isActive();}/*! Returns true if the current query is a \c SELECT statement; otherwise returns false.*/bool QSqlQuery::isSelect() const{ return d->sqlResult->isSelect();}/*! Returns true if you can only scroll forward through a result set; otherwise returns false. \sa setForwardOnly(), next()*/bool QSqlQuery::isForwardOnly() const{ return d->sqlResult->isForwardOnly();}/*! Sets forward only mode to \a forward. If \a forward is true, only next() and seek() with positive values, are allowed for navigating the results. Forward only mode needs far less memory since results do not need to be cached. Forward only mode is off by default. \sa isForwardOnly(), next(), seek()*/void QSqlQuery::setForwardOnly(bool forward){ d->sqlResult->setForwardOnly(forward);}/*! Returns a QSqlRecord containing the field information for the current query. If the query points to a valid row (isValid() returns true), the record is populated with the row's values. An empty record is returned when there is no active query (isActive() returns false). To retrieve values from a query, value() should be used since its index-based lookup is faster. In the following example, a \c{SELECT * FROM} query is executed. Since the order of the columns is not defined, QSqlRecord::indexOf() is used to obtain the index of a column. \code QSqlQuery q("select * from employees"); QSqlRecord rec = q.record(); qDebug() << "Number of columns: " << rec.count(); int nameCol = rec.indexOf("name"); // index of the field "name" while (q.next()) qDebug() << q.value(nameCol).toString(); // output all names \endcode \sa value()*/QSqlRecord QSqlQuery::record() const{ QSqlRecord rec = d->sqlResult->record(); if (isValid()) { for (int i = 0; i < rec.count(); ++i) rec.setValue(i, value(i)); } return rec;}/*! Clears the result set and releases any resources held by the query. You should rarely if ever need to call this function.*/void QSqlQuery::clear(){ *this = QSqlQuery(driver()->createResult());}/*! Prepares the SQL query \a query for execution. The query may contain placeholders for binding values. Both Oracle style colon-name (e.g., \c{:surname}), and ODBC style (\c{?}) placeholders are supported; but they cannot be mixed in the same query. See the \l{QSqlQuery examples}{Detailed Description} for examples. Portability note: Some databases choose to delay preparing a query until it is executed the first time. In this case, preparing a syntactically wrong query succeeds, but every consecutive exec() will fail. \sa exec(), bindValue(), addBindValue()*/bool QSqlQuery::prepare(const QString& query){ if (d->ref != 1) { bool fo = isForwardOnly(); *this = QSqlQuery(driver()->createResult()); setForwardOnly(fo); } else { d->sqlResult->setActive(false); d->sqlResult->setLastError(QSqlError()); d->sqlResult->setAt(QSql::BeforeFirstRow); } if (!driver()) { qWarning("QSqlQuery::prepare: no driver"); return false; } if (!driver()->isOpen() || driver()->isOpenError()) { qWarning("QSqlQuery::prepare: database not open"); return false; } if (query.isEmpty()) { qWarning("QSqlQuery::prepare: empty query"); return false; }#ifdef QT_DEBUG_SQL qDebug("\n QSqlQuery::prepare: %s", query.toLocal8Bit().constData());#endif return d->sqlResult->savePrepare(query);}/*! \overload Executes a previously prepared SQL query. Returns true if the query executed successfully; otherwise returns false. \sa prepare() bindValue() addBindValue() boundValue() boundValues()*/bool QSqlQuery::exec(){ d->sqlResult->resetBindCount(); return d->sqlResult->exec();}/*! Set the placeholder \a placeholder to be bound to value \a val in the prepared statement. Note that the placeholder mark (e.g \c{:}) must be included when specifying the placeholder name. If \a paramType is QSql::Out or QSql::InOut, the placeholder will be overwritten with data from the database after the exec() call. \sa addBindValue(), prepare(), exec(), boundValue() boundValues()*/void QSqlQuery::bindValue(const QString& placeholder, const QVariant& val, QSql::ParamType paramType){ d->sqlResult->bindValue(placeholder, val, paramType);}/*! \overload Set the placeholder in position \a pos to be bound to value \a val in the prepared statement. Field numbering starts at 0. If \a paramType is QSql::Out or QSql::InOut, the placeholder will be overwritten with data from the database after the exec() call.*/void QSqlQuery::bindValue(int pos, const QVariant& val, QSql::ParamType paramType){ d->sqlResult->bindValue(pos, val, paramType);}/*! Adds the value \a val to the list of values when using positional value binding. The order of the addBindValue() calls determines which placeholder a value will be bound to in the prepared query. If \a paramType is QSql::Out or QSql::InOut, the placeholder will be overwritten with data from the database after the exec() call. \sa bindValue(), prepare(), exec(), boundValue() boundValues()*/void QSqlQuery::addBindValue(const QVariant& val, QSql::ParamType paramType){ d->sqlResult->addBindValue(val, paramType);}/*! Returns the value for the \a placeholder. \sa boundValues() bindValue() addBindValue()*/QVariant QSqlQuery::boundValue(const QString& placeholder) const{ return d->sqlResult->boundValue(placeholder);}/*! \overload Returns the value for the placeholder at position \a pos.*/QVariant QSqlQuery::boundValue(int pos) const{ return d->sqlResult->boundValue(pos);}/*! Returns a map of the bound values. With named binding, the bound values can be examined in the following ways: \quotefromfile snippets/sqldatabase/sqldatabase.cpp \skipto examine with named binding \skipto QMapIterator \printuntil } With positional binding, the code becomes: \skipto examine with positional binding \skipto QList \printuntil endl; \sa boundValue() bindValue() addBindValue()*/QMap<QString,QVariant> QSqlQuery::boundValues() const{ QMap<QString,QVariant> map; const QVector<QVariant> values(d->sqlResult->boundValues()); for (int i = 0; i < values.count(); ++i) map[d->sqlResult->boundValueName(i)] = values.at(i); return map;}/*! Returns the last query that was executed. In most cases this function returns the same string as lastQuery(). If a prepared query with placeholders is executed on a DBMS that does not support it, the preparation of this query is emulated. The placeholders in the original query are replaced with their bound values to form a new query. This function returns the modified query. It is mostly useful for debugging purposes. \sa lastQuery()*/QString QSqlQuery::executedQuery() const{ return d->sqlResult->executedQuery();}/*! \fn bool QSqlQuery::prev() Use previous() instead.*//*! Returns the object ID of the most recent inserted row if the database supports it. An invalid QVariant will be returned if the query did not insert any value or if the database does not report the id back. If more than one row was touched by the insert, the behavior is undefined. \sa QSqlDriver::hasFeature()*/QVariant QSqlQuery::lastInsertId() const{ return d->sqlResult->lastInsertId();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -