Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QSqlDriver Class

TheQSqlDriver class is an abstract base class for accessing specific SQL databases.More...

Header:#include <QSqlDriver>
Inherits:QObject

Public Types

enumDriverFeature { Transactions, QuerySize, BLOB, Unicode, ..., MultipleResultSets }
enumIdentifierType { FieldName, TableName }
enumStatementType { WhereStatement, SelectStatement, UpdateStatement, InsertStatement, DeleteStatement }

Public Functions

QSqlDriver(QObject * parent = 0)
~QSqlDriver()
virtual boolbeginTransaction()
virtual voidclose() = 0
virtual boolcommitTransaction()
virtual QSqlResult *createResult() const = 0
virtual QStringescapeIdentifier(const QString & identifier, IdentifierType type) const
virtual QStringformatValue(const QSqlField & field, bool trimStrings = false) const
virtual QVarianthandle() const
virtual boolhasFeature(DriverFeature feature) const = 0
boolisIdentifierEscaped(const QString & identifier, IdentifierType type) const
virtual boolisOpen() const
boolisOpenError() const
QSqlErrorlastError() const
QSql::NumericalPrecisionPolicynumericalPrecisionPolicy() const
virtual boolopen(const QString & db, const QString & user = QString(), const QString & password = QString(), const QString & host = QString(), int port = -1, const QString & options = QString()) = 0
virtual QSqlIndexprimaryIndex(const QString & tableName) const
virtual QSqlRecordrecord(const QString & tableName) const
virtual boolrollbackTransaction()
voidsetNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)
virtual QStringsqlStatement(StatementType type, const QString & tableName, const QSqlRecord & rec, bool preparedStatement) const
QStringstripDelimiters(const QString & identifier, IdentifierType type) const
boolsubscribeToNotification(const QString & name)
QStringListsubscribedToNotifications() const
virtual QStringListtables(QSql::TableType tableType) const
boolunsubscribeFromNotification(const QString & name)
  • 29 public functions inherited fromQObject

Signals

voidnotification(const QString & name)

Protected Functions

virtual voidsetLastError(const QSqlError & error)
virtual voidsetOpen(bool open)
virtual voidsetOpenError(bool error)
  • 8 protected functions inherited fromQObject

Protected Slots

boolisIdentifierEscapedImplementation(const QString & identifier, IdentifierType type) const
QStringstripDelimitersImplementation(const QString & identifier, IdentifierType type) const
boolsubscribeToNotificationImplementation(const QString & name)
QStringListsubscribedToNotificationsImplementation() const
boolunsubscribeFromNotificationImplementation(const QString & name)

Additional Inherited Members

  • 1 property inherited fromQObject
  • 1 public slot inherited fromQObject
  • 7 static public members inherited fromQObject

Detailed Description

TheQSqlDriver class is an abstract base class for accessing specific SQL databases.

This class should not be used directly. UseQSqlDatabase instead.

If you want to create your own SQL drivers, you can subclass this class and reimplement its pure virtual functions and those virtual functions that you need. SeeHow to Write Your Own Database Driver for more information.

See alsoQSqlDatabase andQSqlResult.

Member Type Documentation

enum QSqlDriver::DriverFeature

This enum contains a list of features a driver might support. UsehasFeature() to query whether a feature is supported or not.

ConstantValueDescription
QSqlDriver::Transactions0Whether the driver supports SQL transactions.
QSqlDriver::QuerySize1Whether the database is capable of reporting the size of a query. Note that some databases do not support returning the size (i.e. number of rows returned) of a query, in which caseQSqlQuery::size() will return -1.
QSqlDriver::BLOB2Whether the driver supports Binary Large Object fields.
QSqlDriver::Unicode3Whether the driver supports Unicode strings if the database server does.
QSqlDriver::PreparedQueries4Whether the driver supports prepared query execution.
QSqlDriver::NamedPlaceholders5Whether the driver supports the use of named placeholders.
QSqlDriver::PositionalPlaceholders6Whether the driver supports the use of positional placeholders.
QSqlDriver::LastInsertId7Whether the driver supports returning the Id of the last touched row.
QSqlDriver::BatchOperations8Whether the driver supports batched operations, seeQSqlQuery::execBatch()
QSqlDriver::SimpleLocking9Whether the driver disallows a write lock on a table while other queries have a read lock on it.
QSqlDriver::LowPrecisionNumbers10Whether the driver allows fetching numerical values with low precision.
QSqlDriver::EventNotifications11Whether the driver supports database event notifications.
QSqlDriver::FinishQuery12Whether the driver can do any low-level resource cleanup whenQSqlQuery::finish() is called.
QSqlDriver::MultipleResultSets13Whether the driver can access multiple result sets returned from batched statements or stored procedures.

More information about supported features can be found in theQt SQL driver documentation.

See alsohasFeature().

enum QSqlDriver::IdentifierType

This enum contains a list of SQL identifier types.

ConstantValueDescription
QSqlDriver::FieldName0A SQL field name
QSqlDriver::TableName1A SQL table name

enum QSqlDriver::StatementType

This enum contains a list of SQL statement (or clause) types the driver can create.

ConstantValueDescription
QSqlDriver::WhereStatement0An SQLWHERE statement (e.g.,WHERE f = 5).
QSqlDriver::SelectStatement1An SQLSELECT statement (e.g.,SELECT f FROM t).
QSqlDriver::UpdateStatement2An SQLUPDATE statement (e.g.,UPDATE TABLE t set f = 1).
QSqlDriver::InsertStatement3An SQLINSERT statement (e.g.,INSERT INTO t (f) values (1)).
QSqlDriver::DeleteStatement4An SQLDELETE statement (e.g.,DELETE FROM t).

See alsosqlStatement().

Member Function Documentation

QSqlDriver::QSqlDriver(QObject * parent = 0)

Constructs a new driver with the givenparent.

QSqlDriver::~QSqlDriver()

Destroys the object and frees any allocated resources.

[virtual]bool QSqlDriver::beginTransaction()

This function is called to begin a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See alsocommitTransaction() androllbackTransaction().

[pure virtual]void QSqlDriver::close()

Derived classes must reimplement this pure virtual function in order to close the database connection. Return true on success, false on failure.

See alsoopen() andsetOpen().

[virtual]bool QSqlDriver::commitTransaction()

This function is called to commit a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See alsobeginTransaction() androllbackTransaction().

[pure virtual]QSqlResult * QSqlDriver::createResult() const

Creates an empty SQL result on the database. Derived classes must reimplement this function and return aQSqlResult object appropriate for their database to the caller.

[virtual]QString QSqlDriver::escapeIdentifier(constQString & identifier,IdentifierType type) const

Returns theidentifier escaped according to the database rules.identifier can either be a table name or field name, dependent ontype.

The default implementation does nothing.

See alsoisIdentifierEscaped().

[virtual]QString QSqlDriver::formatValue(constQSqlField & field,bool trimStrings = false) const

Returns a string representation of thefield value for the database. This is used, for example, when constructing INSERT and UPDATE statements.

The default implementation returns the value formatted as a string according to the following rules:

  • Iffield is character data, the value is returned enclosed in single quotation marks, which is appropriate for many SQL databases. Any embedded single-quote characters are escaped (replaced with two single-quote characters). IftrimStrings is true (the default is false), all trailing whitespace is trimmed from the field.
  • Iffield is date/time data, the value is formatted in ISO format and enclosed in single quotation marks. If the date/time data is invalid, "NULL" is returned.
  • Iffield isbytearray data, and the driver can edit binary fields, the value is formatted as a hexadecimal string.
  • For any other field type, toString() is called on its value and the result of this is returned.

See alsoQVariant::toString().

[virtual]QVariant QSqlDriver::handle() const

Returns the low-level database handle wrapped in aQVariant or an invalid variant if there is no handle.

Warning: Use this with uttermost care and only if you know what you're doing.

Warning: The handle returned here can become a stale pointer if the connection is modified (for example, if you close the connection).

Warning: The handle can be NULL if the connection is not open yet.

The handle returned here is database-dependent, you should query the type name of the variant before accessing it.

This example retrieves the handle for a connection to sqlite:

QSqlDatabase db=...;QVariant v= db.driver()->handle();if (v.isValid()&& qstrcmp(v.typeName(),"sqlite3*")==0) {// v.data() returns a pointer to the handle    sqlite3*handle=*static_cast<sqlite3**>(v.data());if (handle!=0) {// check that it is not NULL...    }}

This snippet returns the handle for PostgreSQL or MySQL:

if (qstrcmp(v.typeName(),"PGconn*")==0) {    PGconn*handle=*static_cast<PGconn**>(v.data());if (handle!=0)...}if (qstrcmp(v.typeName(),"MYSQL*")==0) {    MYSQL*handle=*static_cast<MYSQL**>(v.data());if (handle!=0)...}

See alsoQSqlResult::handle().

[pure virtual]bool QSqlDriver::hasFeature(DriverFeature feature) const

Returns true if the driver supports featurefeature; otherwise returns false.

Note that some databases need to beopen() before this can be determined.

See alsoDriverFeature.

bool QSqlDriver::isIdentifierEscaped(constQString & identifier,IdentifierType type) const

Returns whetheridentifier is escaped according to the database rules.identifier can either be a table name or field name, dependent ontype.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide your own implementation in yourQSqlDriver subclass, reimplement theisIdentifierEscapedImplementation() slot in your subclass instead. The isIdentifierEscapedFunction() will dynamically detect the slot and call it.

See alsostripDelimiters() andescapeIdentifier().

[protected slot]bool QSqlDriver::isIdentifierEscapedImplementation(constQString & identifier,IdentifierType type) const

This slot returns whetheridentifier is escaped according to the database rules.identifier can either be a table name or field name, dependent ontype.

Because of binary compatibility constraints,isIdentifierEscaped() function (introduced in Qt 4.5) is not virtual. Instead,isIdentifierEscaped() will dynamically detect and callthis slot. The default implementation assumes the escape/delimiter character is a double quote. Reimplement this slot in your ownQSqlDriver if your database engine uses a different delimiter character.

This function was introduced in Qt 4.6.

See alsoisIdentifierEscaped().

[virtual]bool QSqlDriver::isOpen() const

Returns true if the database connection is open; otherwise returns false.

bool QSqlDriver::isOpenError() const

Returns true if the there was an error opening the database connection; otherwise returns false.

QSqlError QSqlDriver::lastError() const

Returns aQSqlError object which contains information about the last error that occurred on the database.

See alsosetLastError().

[signal]void QSqlDriver::notification(constQString & name)

This signal is emitted when the database posts an event notification that the driver subscribes to.name identifies the event notification.

This function was introduced in Qt 4.4.

See alsosubscribeToNotification().

QSql::NumericalPrecisionPolicy QSqlDriver::numericalPrecisionPolicy() const

Returns the current default precision policy for the database connection.

This function was introduced in Qt 4.6.

See alsoQSql::NumericalPrecisionPolicy,setNumericalPrecisionPolicy(),QSqlQuery::numericalPrecisionPolicy(), andQSqlQuery::setNumericalPrecisionPolicy().

[pure virtual]bool QSqlDriver::open(constQString & db, constQString & user = QString(), constQString & password = QString(), constQString & host = QString(),int port = -1, constQString & options = QString())

Derived classes must reimplement this pure virtual function to open a database connection on databasedb, using user nameuser, passwordpassword, hosthost, portport and connection optionsoptions.

The function must return true on success and false on failure.

See alsosetOpen().

[virtual]QSqlIndex QSqlDriver::primaryIndex(constQString & tableName) const

Returns the primary index for tabletableName. Returns an emptyQSqlIndex if the table doesn't have a primary index. The default implementation returns an empty index.

[virtual]QSqlRecord QSqlDriver::record(constQString & tableName) const

Returns aQSqlRecord populated with the names of the fields in tabletableName. If no such table exists, an empty record is returned. The default implementation returns an empty record.

[virtual]bool QSqlDriver::rollbackTransaction()

This function is called to rollback a transaction. If successful, return true, otherwise return false. The default implementation does nothing and returns false.

See alsobeginTransaction() andcommitTransaction().

[virtual protected]void QSqlDriver::setLastError(constQSqlError & error)

This function is used to set the value of the last error,error, that occurred on the database.

See alsolastError().

void QSqlDriver::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)

Sets the default numerical precision policy used by queries created by this driver toprecisionPolicy.

Note: Setting the default precision policy toprecisionPolicy doesn't affect any currently active queries.

This function was introduced in Qt 4.6.

See alsoQSql::NumericalPrecisionPolicy,numericalPrecisionPolicy(),QSqlQuery::setNumericalPrecisionPolicy(), andQSqlQuery::numericalPrecisionPolicy().

[virtual protected]void QSqlDriver::setOpen(bool open)

This function sets the open state of the database toopen. Derived classes can use this function to report the status ofopen().

See alsoopen() andsetOpenError().

[virtual protected]void QSqlDriver::setOpenError(bool error)

This function sets the open error state of the database toerror. Derived classes can use this function to report the status ofopen(). Note that iferror is true the open state of the database is set to closed (i.e.,isOpen() returns false).

See alsoisOpenError(),open(), andsetOpen().

[virtual]QString QSqlDriver::sqlStatement(StatementType type, constQString & tableName, constQSqlRecord & rec,bool preparedStatement) const

Returns a SQL statement of typetype for the tabletableName with the values fromrec. IfpreparedStatement is true, the string will contain placeholders instead of values.

This method can be used to manipulate tables without having to worry about database-dependent SQL dialects. For non-prepared statements, the values will be properly escaped.

QString QSqlDriver::stripDelimiters(constQString & identifier,IdentifierType type) const

Returns theidentifier with the leading and trailing delimiters removed,identifier can either be a table name or field name, dependent ontype. Ifidentifier does not have leading and trailing delimiter characters,identifier is returned without modification.

Warning: Because of binary compatibility constraints, this function is not virtual, If you want to provide your own implementation in yourQSqlDriver subclass, reimplement thestripDelimitersImplementation() slot in your subclass instead. The stripDelimiters() function will dynamically detect the slot and call it.

This function was introduced in Qt 4.5.

See alsoisIdentifierEscaped().

[protected slot]QString QSqlDriver::stripDelimitersImplementation(constQString & identifier,IdentifierType type) const

This slot returnsidentifier with the leading and trailing delimiters removed,identifier can either be a tablename or field name, dependent ontype. Ifidentifier does not have leading and trailing delimiter characters,identifier is returned without modification.

Because of binary compatibility constraints, thestripDelimiters() function (introduced in Qt 4.5) is not virtual. Instead,stripDelimiters() will dynamically detect and callthis slot. It generally unnecessary to reimplement this slot.

This function was introduced in Qt 4.6.

See alsostripDelimiters().

bool QSqlDriver::subscribeToNotification(constQString & name)

This function is called to subscribe to event notifications from the database.name identifies the event notification.

If successful, return true, otherwise return false.

The database must be open when this function is called. When the database is closed by callingclose() all subscribed event notifications are automatically unsubscribed. Note that callingopen() on an already open database may implicitly causeclose() to be called, which will cause the driver to unsubscribe from all event notifications.

When an event notification identified byname is posted by the database thenotification() signal is emitted.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide event notification support in your ownQSqlDriver subclass, reimplement thesubscribeToNotificationImplementation() slot in your subclass instead. The subscribeToNotification() function will dynamically detect the slot and call it.

This function was introduced in Qt 4.4.

See alsounsubscribeFromNotification(),subscribedToNotifications(), andQSqlDriver::hasFeature().

[protected slot]bool QSqlDriver::subscribeToNotificationImplementation(constQString & name)

This slot is called to subscribe to event notifications from the database.name identifies the event notification.

If successful, return true, otherwise return false.

The database must be open when thisslot is called. When the database is closed by callingclose() all subscribed event notifications are automatically unsubscribed. Note that callingopen() on an already open database may implicitly causeclose() to be called, which will cause the driver to unsubscribe from all event notifications.

When an event notification identified byname is posted by the database thenotification() signal is emitted.

Reimplement this slot to provide your ownQSqlDriver subclass with event notification support; because of binary compatibility constraints, thesubscribeToNotification() function (introduced in Qt 4.4) is not virtual. Instead,subscribeToNotification() will dynamically detect and callthis slot. The default implementation does nothing and returns false.

This function was introduced in Qt 4.4.

See alsosubscribeToNotification().

QStringList QSqlDriver::subscribedToNotifications() const

Returns a list of the names of the event notifications that are currently subscribed to.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide event notification support in your ownQSqlDriver subclass, reimplement thesubscribedToNotificationsImplementation() slot in your subclass instead. The subscribedToNotifications() function will dynamically detect the slot and call it.

This function was introduced in Qt 4.4.

See alsosubscribeToNotification() andunsubscribeFromNotification().

[protected slot]QStringList QSqlDriver::subscribedToNotificationsImplementation() const

Returns a list of the names of the event notifications that are currently subscribed to.

Reimplement this slot to provide your ownQSqlDriver subclass with event notification support; because of binary compatibility constraints, thesubscribedToNotifications() function (introduced in Qt 4.4) is not virtual. Instead,subscribedToNotifications() will dynamically detect and callthis slot. The default implementation simply returns an emptyQStringList.

This function was introduced in Qt 4.4.

See alsosubscribedToNotifications().

[virtual]QStringList QSqlDriver::tables(QSql::TableType tableType) const

Returns a list of the names of the tables in the database. The default implementation returns an empty list.

ThetableType argument describes what types of tables should be returned. Due to binary compatibility, the string contains the value of the enum QSql::TableTypes as text. An empty string should be treated asQSql::Tables for backward compatibility.

bool QSqlDriver::unsubscribeFromNotification(constQString & name)

This function is called to unsubscribe from event notifications from the database.name identifies the event notification.

If successful, return true, otherwise return false.

The database must be open when this function is called. All subscribed event notifications are automatically unsubscribed from when theclose() function is called.

After callingthis function thenotification() signal will no longer be emitted when an event notification identified byname is posted by the database.

Warning: Because of binary compatibility constraints, this function is not virtual. If you want to provide event notification support in your ownQSqlDriver subclass, reimplement theunsubscribeFromNotificationImplementation() slot in your subclass instead. The unsubscribeFromNotification() function will dynamically detect the slot and call it.

This function was introduced in Qt 4.4.

See alsosubscribeToNotification() andsubscribedToNotifications().

[protected slot]bool QSqlDriver::unsubscribeFromNotificationImplementation(constQString & name)

This slot is called to unsubscribe from event notifications from the database.name identifies the event notification.

If successful, return true, otherwise return false.

The database must be open whenthis slot is called. All subscribed event notifications are automatically unsubscribed from when theclose() function is called.

After callingthis slot thenotification() signal will no longer be emitted when an event notification identified byname is posted by the database.

Reimplement this slot to provide your ownQSqlDriver subclass with event notification support; because of binary compatibility constraints, theunsubscribeFromNotification() function (introduced in Qt 4.4) is not virtual. Instead,unsubscribeFromNotification() will dynamically detect and callthis slot. The default implementation does nothing and returns false.

This function was introduced in Qt 4.4.

See alsounsubscribeFromNotification().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp