返回介绍

QSqlDatabase Class

发布于 2019-10-04 15:02:52 字数 15773 浏览 1004 评论 0 收藏 0

The QSqlDatabase class is used to create SQL database connections and provide transaction handling. More...

#include <qsqldatabase.h>

Inherits QObject.

List of all member functions.

Public Members

  • ~QSqlDatabase ()
  • bool open ()
  • bool open ( constQString&user, constQString&password )
  • void close ()
  • bool isOpen () const
  • bool isOpenError () const
  • QStringList tables () const
  • QSqlIndex primaryIndex ( constQString&tablename ) const
  • QSqlRecord record ( constQString&tablename ) const
  • QSqlRecord record ( constQSqlQuery&query ) const
  • QSqlRecordInfo recordInfo ( constQString&tablename ) const
  • QSqlRecordInfo recordInfo ( constQSqlQuery&query ) const
  • QSqlQuery exec ( constQString&query = QString::null ) const
  • QSqlError lastError () const
  • bool transaction ()
  • bool commit ()
  • bool rollback ()
  • virtual void setDatabaseName ( constQString&name )
  • virtual void setUserName ( constQString&name )
  • virtual void setPassword ( constQString&password )
  • virtual void setHostName ( constQString&host )
  • virtual void setPort ( intp )
  • QString databaseName () const
  • QString userName () const
  • QString password () const
  • QString hostName () const
  • QString driverName () const
  • int port () const
  • QSqlDriver * driver () const

Static Public Members

  • QSqlDatabase * addDatabase ( constQString&type, constQString&connectionName = defaultConnection )
  • QSqlDatabase * database ( constQString&connectionName = defaultConnection, boolopen = TRUE )
  • void removeDatabase ( constQString&connectionName )
  • bool contains ( constQString&connectionName = defaultConnection )
  • QStringList drivers ()

Properties

  • QString databaseName- the name of the database. Note that the database name is the TNS Service Name for the QOCI8 (Oracle) driver, and the Data Source Name for the QODBC3 driver
  • QString hostName- the host name where the database resides
  • QString password- the password used to connect to the database
  • int port- the port used to connect to the database
  • QString userName- the user name connected to the database

Protected Members

  • QSqlDatabase ( constQString&driver, constQString&name, QObject*parent = 0, constchar*objname = 0 )

Detailed Description

The QSqlDatabase class is used to create SQL database connections and provide transaction handling.

This class is used to create connections to SQL databases. It also provides transaction handling functions for those database drivers that support transactions.

The QSqlDatabase class itself provides an abstract interface for accessing many types of database backend. Database-specific drivers are used internally to actually access and manipulate data, (see QSqlDriver). Result set objects provide the interface for executing and manipulating SQL queries (see QSqlQuery).

See also Database Classes.


Member Function Documentation

QSqlDatabase::QSqlDatabase ( constQString&driver, constQString&name, QObject*parent = 0, constchar*objname = 0 ) [protected]

Creates a QSqlDatabase connection named name that uses the driver referred to by driver, with the parent parent and the object name objname. If the driver is not recognized, the database connection will have no functionality.

The currently available drivers are:

  • QODBC3 - ODBC (Open Database Connectivity) Driver
  • QOCI8 - Oracle Call Interface Driver
  • QPSQL7 - PostgreSQL v6.x and v7.x Driver
  • QTDS7 - Sybase Adaptive Server and Microsoft SQL Server Driver
  • QMYSQL3 - MySQL Driver

Note that additional 3rd party drivers can be loaded dynamically.

QSqlDatabase::~QSqlDatabase ()

Destroys the object and frees any allocated resources.

QSqlDatabase* QSqlDatabase::addDatabase ( constQString&type, constQString&connectionName = defaultConnection ) [static]

Adds a database to the list of database connections using the driver type and the connection name connectionName.

The database connection is referred to by connectionName. A pointer to the newly added database connection is returned. This pointer is owned by QSqlDatabase and will be deleted on program exit or when removeDatabase() is called. If connectionName is not specified, the newly added database connection becomes the default database connection for the application, and subsequent calls to database() (without a database name parameter) will return a pointer to it.

See also database() and removeDatabase().

Examples: sql/overview/connect1/main.cpp, sql/overview/create_connections/main.cpp and sql/sqltable/main.cpp.

void QSqlDatabase::close ()

Closes the database connection, freeing any resources acquired.

bool QSqlDatabase::commit ()

Commits a transaction to the database if the driver supports transactions. Returns TRUE if the operation succeeded, FALSE otherwise.

See also QSqlDriver::hasFeature() and rollback().

bool QSqlDatabase::contains ( constQString&connectionName = defaultConnection ) [static]

Returns TRUE if the list of database connections contains connectionName, otherwise returns FALSE.

QSqlDatabase* QSqlDatabase::database ( constQString&connectionName = defaultConnection, boolopen = TRUE ) [static]

Returns a pointer to the database connection named connectionName. The database connection must have been previously added with database(). If open is TRUE (the default) and the database connection is not already open it is opened now. If no connectionName is specified the default connection is used. If connectionName does not exist in the list of databases, 0 is returned. The pointer returned is owned by QSqlDatabase and should not be deleted.

Examples: sql/overview/basicbrowsing/main.cpp and sql/overview/create_connections/main.cpp.

QString QSqlDatabase::databaseName () const

Returns the name of the database. Note that the database name is the TNS Service Name for the QOCI8 (Oracle) driver, and the Data Source Name for the QODBC3 driver. See the "databaseName" property for details.

QSqlDriver* QSqlDatabase::driver () const

Returns a pointer to the database driver used to access the database connection.

QString QSqlDatabase::driverName () const

Returns the name of the driver used by the database connection.

QStringList QSqlDatabase::drivers () [static]

Returns a list of all available database drivers.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

    QStringList list = myDatabase.drivers();
    QStringList::Iterator it = list.begin();
    while( it != list.end() ) {
        myProcessing( *it );
        ++it;
    }
    

QSqlQuery QSqlDatabase::exec ( constQString&query = QString::null ) const

Executes an SQL statement (e.g. an INSERT, UPDATE or DELETE statement) on the database, and returns a QSqlQuery object. Use lastError() to retrieve error information. If query is QString::null, an empty, invalid query is returned and lastError() is not affected.

See also QSqlQuery and lastError().

QString QSqlDatabase::hostName () const

Returns the host name where the database resides. See the "hostName" property for details.

bool QSqlDatabase::isOpen () const

Returns TRUE if the database connection is currently open, otherwise returns FALSE.

bool QSqlDatabase::isOpenError () const

Returns TRUE if there was an error opening the database connection, otherwise returns FALSE. Error information can be retrieved using the lastError() function.

QSqlError QSqlDatabase::lastError () const

Returns information about the last error that occurred on the database. See QSqlError for more information.

Examples: sql/overview/create_connections/main.cpp and sql/sqltable/main.cpp.

bool QSqlDatabase::open ()

Opens the database connection using the current connection values. Returns TRUE on success, and FALSE if there was an error. Error information can be retrieved using the lastError() function.

See also lastError().

Examples: sql/overview/connect1/main.cpp, sql/overview/create_connections/main.cpp and sql/sqltable/main.cpp.

bool QSqlDatabase::open ( constQString&user, constQString&password )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Opens the database connection using user name and password. Returns TRUE on success, and FALSE if there was an error. Error information can be retrieved using the lastError() function.

See also lastError().

QString QSqlDatabase::password () const

Returns the password used to connect to the database. See the "password" property for details.

int QSqlDatabase::port () const

Returns the port used to connect to the database. See the "port" property for details.

QSqlIndex QSqlDatabase::primaryIndex ( constQString&tablename ) const

Returns the primary index for table tablename. If no primary index exists an empty QSqlIndex will be returned.

QSqlRecord QSqlDatabase::record ( constQString&tablename ) const

Returns a QSqlRecord populated with the names of all the fields in the table (or view) named tablename. The order in which the fields are returned is undefined. If no such table (or view) exists, an empty record is returned.

See also recordInfo().

QSqlRecord QSqlDatabase::record ( constQSqlQuery&query ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a QSqlRecord populated with the names of all the fields used in the SQL query. If the query is a "SELECT *" the order in which fields are returned is undefined.

See also recordInfo().

QSqlRecordInfo QSqlDatabase::recordInfo ( constQString&tablename ) const

Returns a QSqlRecordInfo populated with meta-data about the table (or view) tablename. If no such table (or view) exists, an empty record is returned.

See also QSqlRecordInfo, QSqlFieldInfo and record().

QSqlRecordInfo QSqlDatabase::recordInfo ( constQSqlQuery&query ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a QSqlRecordInfo object with meta data for the QSqlQuery query. Note that this overloaded function may return not as much information as the recordInfo function which takes the name of a table as parameter.

See also QSqlRecordInfo, QSqlFieldInfo and record().

void QSqlDatabase::removeDatabase ( constQString&connectionName ) [static]

Removes the database connection connectionName from the list of database connections. Note that there should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.

bool QSqlDatabase::rollback ()

Rolls a transaction back on the database if the driver supports transactions. Returns TRUE if the operation succeeded, FALSE otherwise.

See also QSqlDriver::hasFeature(), commit() and transaction().

void QSqlDatabase::setDatabaseName ( constQString&name ) [virtual]

Sets the name of the database. Note that the database name is the TNS Service Name for the QOCI8 (Oracle) driver, and the Data Source Name for the QODBC3 driver to name. See the "databaseName" property for details.

void QSqlDatabase::setHostName ( constQString&host ) [virtual]

Sets the host name where the database resides to host. See the "hostName" property for details.

void QSqlDatabase::setPassword ( constQString&password ) [virtual]

Sets the password used to connect to the database to password. See the "password" property for details.

void QSqlDatabase::setPort ( intp ) [virtual]

Sets the port used to connect to the database to p. See the "port" property for details.

void QSqlDatabase::setUserName ( constQString&name ) [virtual]

Sets the user name connected to the database to name. See the "userName" property for details.

QStringList QSqlDatabase::tables () const

Returns a list of tables in the database.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

    QStringList list = myDatabase.tables();
    QStringList::Iterator it = list.begin();
    while( it != list.end() ) {
        myProcessing( *it );
        ++it;
    }
    

bool QSqlDatabase::transaction ()

Begins a transaction on the database if the driver supports transactions. Returns TRUE if the operation succeeded, FALSE otherwise.

See also QSqlDriver::hasFeature(), commit() and rollback().

QString QSqlDatabase::userName () const

Returns the user name connected to the database. See the "userName" property for details.


Property Documentation

QString databaseName

This property holds the name of the database. Note that the database name is the TNS Service Name for the QOCI8 (Oracle) driver, and the Data Source Name for the QODBC3 driver.

There is no default value.

Set this property's value with setDatabaseName() and get this property's value with databaseName().

QString hostName

This property holds the host name where the database resides.

There is no default value.

Set this property's value with setHostName() and get this property's value with hostName().

QString password

This property holds the password used to connect to the database.

There is no default value.

Set this property's value with setPassword() and get this property's value with password().

int port

This property holds the port used to connect to the database.

There is no default value.

Set this property's value with setPort() and get this property's value with port().

QString userName

This property holds the user name connected to the database.

There is no default value.

Set this property's value with setUserName() and get this property's value with userName().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文