⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsqldatabase.3qt

📁 Trolltech公司发布的基于C++图形开发环境
💻 3QT
📖 第 1 页 / 共 2 页
字号:
'\" t.TH QSqlDatabase 3qt "21 January 2005" "Trolltech AS" \" -*- nroff -*-.\" Copyright 1992-2001 Trolltech AS.  All rights reserved.  See the.\" license file included in the distribution for a complete license.\" statement..\".ad l.nh.SH NAMEQSqlDatabase \- Used to create SQL database connections and to provide transaction handling.SH SYNOPSIS\fC#include <qsqldatabase.h>\fR.PPInherits QObject..PP.SS "Public Members".in +1c.ti -1c.BI "\fB~QSqlDatabase\fR ()".br.ti -1c.BI "bool \fBopen\fR ()".br.ti -1c.BI "bool \fBopen\fR ( const QString & user, const QString & password )".br.ti -1c.BI "void \fBclose\fR ()".br.ti -1c.BI "bool \fBisOpen\fR () const".br.ti -1c.BI "bool \fBisOpenError\fR () const".br.ti -1c.BI "QStringList \fBtables\fR () const".br.ti -1c.BI "QStringList \fBtables\fR ( QSql::TableType type ) const".br.ti -1c.BI "QSqlIndex \fBprimaryIndex\fR ( const QString & tablename ) const".br.ti -1c.BI "QSqlRecord \fBrecord\fR ( const QString & tablename ) const".br.ti -1c.BI "QSqlRecord \fBrecord\fR ( const QSqlQuery & query ) const".br.ti -1c.BI "QSqlRecordInfo \fBrecordInfo\fR ( const QString & tablename ) const".br.ti -1c.BI "QSqlRecordInfo \fBrecordInfo\fR ( const QSqlQuery & query ) const".br.ti -1c.BI "QSqlQuery \fBexec\fR ( const QString & query = QString::null ) const".br.ti -1c.BI "QSqlError \fBlastError\fR () const".br.ti -1c.BI "bool \fBtransaction\fR ()".br.ti -1c.BI "bool \fBcommit\fR ()".br.ti -1c.BI "bool \fBrollback\fR ()".br.ti -1c.BI "virtual void \fBsetDatabaseName\fR ( const QString & name )".br.ti -1c.BI "virtual void \fBsetUserName\fR ( const QString & name )".br.ti -1c.BI "virtual void \fBsetPassword\fR ( const QString & password )".br.ti -1c.BI "virtual void \fBsetHostName\fR ( const QString & host )".br.ti -1c.BI "virtual void \fBsetPort\fR ( int p )".br.ti -1c.BI "void \fBsetConnectOptions\fR ( const QString & options = QString::null )".br.ti -1c.BI "QString \fBdatabaseName\fR () const".br.ti -1c.BI "QString \fBuserName\fR () const".br.ti -1c.BI "QString \fBpassword\fR () const".br.ti -1c.BI "QString \fBhostName\fR () const".br.ti -1c.BI "QString \fBdriverName\fR () const".br.ti -1c.BI "int \fBport\fR () const".br.ti -1c.BI "QString \fBconnectOptions\fR () const".br.ti -1c.BI "QSqlDriver * \fBdriver\fR () const".br.in -1c.SS "Static Public Members".in +1c.ti -1c.BI "QSqlDatabase * \fBaddDatabase\fR ( const QString & type, const QString & connectionName = defaultConnection )".br.ti -1c.BI "QSqlDatabase * \fBaddDatabase\fR ( QSqlDriver * driver, const QString & connectionName = defaultConnection )".br.ti -1c.BI "QSqlDatabase * \fBdatabase\fR ( const QString & connectionName = defaultConnection, bool open = TRUE )".br.ti -1c.BI "void \fBremoveDatabase\fR ( const QString & connectionName )".br.ti -1c.BI "void \fBremoveDatabase\fR ( QSqlDatabase * db )".br.ti -1c.BI "bool \fBcontains\fR ( const QString & connectionName = defaultConnection )".br.ti -1c.BI "QStringList \fBdrivers\fR ()".br.ti -1c.BI "void \fBregisterSqlDriver\fR ( const QString & name, const QSqlDriverCreatorBase * creator )".br.ti -1c.BI "bool \fBisDriverAvailable\fR ( const QString & name )".br.in -1c.SS "Properties".in +1c.ti -1c.BI "QString \fBconnectOptions\fR - the database connect options".br.ti -1c.BI "QString \fBdatabaseName\fR - the name of the database".br.ti -1c.BI "QString \fBhostName\fR - the host name where the database resides".br.ti -1c.BI "QString \fBpassword\fR - the password used to connect to the database".br.ti -1c.BI "int \fBport\fR - the port used to connect to the database".br.ti -1c.BI "QString \fBuserName\fR - the user name connected to the database".br.in -1c.SS "Protected Members".in +1c.ti -1c.BI "\fBQSqlDatabase\fR ( const QString & type, const QString & name, QObject * parent = 0, const char * objname = 0 )".br.ti -1c.BI "\fBQSqlDatabase\fR ( QSqlDriver * driver, QObject * parent = 0, const char * objname = 0 )".br.in -1c.SH DESCRIPTIONThe QSqlDatabase class is used to create SQL database connections and to provide transaction handling..PPNote that transaction handling is not supported by every SQL database. You can find out whether transactions are supported using QSqlDriver::hasFeature()..PPThe QSqlDatabase class provides an abstract interface for accessing many types of database backends. 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)..PPSee also Database Classes..SH MEMBER FUNCTION DOCUMENTATION.SH "QSqlDatabase::QSqlDatabase ( const QString & type, const QString & name, QObject * parent = 0, const char * objname = 0 )\fC [protected]\fR"Creates a QSqlDatabase connection called \fIname\fR that uses the driver referred to by \fItype\fR, with the parent \fIparent\fR and the object name \fIobjname\fR. If the \fItype\fR is not recognized, the database connection will have no functionality..PPThe currently available drivers are:.PP<center>.nf.TSl - l. Driver Type Description QODBC3 ODBC Driver (includes Microsoft SQL Server) QOCI8 Oracle Call Interface Driver QPSQL7 PostgreSQL v6.x and v7.x Driver QTDS7 Sybase Adaptive Server QMYSQL3 MySQL Driver QDB2 IBM DB2, v7.1 and higher QSQLITE SQLite Driver QIBASE.TE.fi</center>.PPAdditional third party drivers, including your own custom drivers, can be loaded dynamically..PPSee also registerSqlDriver()..SH "QSqlDatabase::QSqlDatabase ( QSqlDriver * driver, QObject * parent = 0, const char * objname = 0 )\fC [protected]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPCreates a database connection using the driver \fIdriver\fR, with the parent \fIparent\fR and the object name \fIobjname\fR..PP\fBWarning:\fR The framework takes ownership of the \fIdriver\fR pointer, so it should not be deleted..SH "QSqlDatabase::~QSqlDatabase ()"Destroys the object and frees any allocated resources..SH "QSqlDatabase * QSqlDatabase::addDatabase ( const QString & type, const QString & connectionName = defaultConnection )\fC [static]\fR"Adds a database to the list of database connections using the driver \fItype\fR and the connection name \fIconnectionName\fR..PPThe database connection is referred to by \fIconnectionName\fR. 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..PPIf \fIconnectionName\fR 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. If \fIconnectionName\fR is given, use database(connectionName) to retrieve a pointer to the database connection..PPSee also database() and removeDatabase()..PPExamples:.)l sql/overview/connect1/main.cpp, sql/overview/connection.cpp, and sql/sqltable/main.cpp..SH "QSqlDatabase * QSqlDatabase::addDatabase ( QSqlDriver * driver, const QString & connectionName = defaultConnection )\fC [static]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPThis function is useful if you need to set up the database connection and instantiate the driver yourself. If you do this, it is recommended that you include the driver code in your own application. For example, setting up a custom PostgreSQL connection and instantiating the QPSQL7 driver can be done the following way:.PP.nf.br    #include "qtdir/src/sql/drivers/psql/qsql_psql.cpp".br.fi(We assume that \fCqtdir\fR is the directory where Qt is installed.) This will pull in the code that is needed to use the PostgreSQL client library and to instantiate a QPSQLDriver object, assuming that you have the PostgreSQL headers somewhere in your include search path..PP.nf.br    PGconn* con = PQconnectdb( "host=server user=bart password=simpson dbname=springfield" );.br    QPSQLDriver* drv =  new QPSQLDriver( con );.br    QSqlDatabase* db = QSqlDatabase::addDatabase( drv ); // becomes the new default connection.br    QSqlQuery q;.br    q.exec( "SELECT * FROM people" );.br    ....br.fi.PPThe above code sets up a PostgreSQL connection and instantiates a QPSQLDriver object. Next, addDatabase() is called to add the connection to the known connections so that it can be used by the Qt SQL classes. When a driver is instantiated with a connection handle (or set of handles), Qt assumes that you have already opened the database connection..PPRemember that you must link your application against the database client library as well. The simplest way to do this is to add lines like those below to your \fC.pro\fR file:.PP.nf.br    unix:LIBS += -lpq.br    win32:LIBS += libpqdll.lib.br.fi.PPYou will need to have the client library in your linker's search path..PPThe method described above will work for all the drivers, the only difference is the arguments the driver constructors take. Below is an overview of the drivers and their constructor arguments..PP<center>.nf.TSl - l. Driver Class name Constructor arguments File to include QPSQL7 QPSQLDriver PGconn* connection QMYSQL3 QMYSQLDriver MYSQL* connection QOCI8 QOCIDriver OCIEnv* environment, OCIError* error, OCISvcCtx* serviceContext QODBC3 QODBCDriver SQLHANDLE environment, SQLHANDLE connection QDB2 QDB2 SQLHANDLE environment, SQLHANDLE connection QTDS7 QTDSDriver LOGINREC* loginRecord, DBPROCESS* dbProcess, const QString& hostName QSQLITE QSQLiteDriver sqlite* connection QIBASE QIBaseDriver isc_db_handle connection .TE.fi</center>.PPNote: The host name (or service name) is needed when constructing the QTDSDriver for creating new connections for internal queries. This is to prevent the simultaneous usage of several QSqlQuery/QSqlCursor objects from blocking each other..PP\fBWarning:\fR The SQL framework takes ownership of the \fIdriver\fR pointer, and it should not be deleted. The returned QSqlDatabase object is owned by the framework and must not be deleted. If you want to explicitly remove the connection, use removeDatabase().PPSee also drivers()..SH "void QSqlDatabase::close ()"Closes the database connection, freeing any resources acquired..PPSee also removeDatabase()..SH "bool QSqlDatabase::commit ()"Commits a transaction to the database if the driver supports transactions. Returns TRUE if the operation succeeded; otherwise returns FALSE..PPSee also QSqlDriver::hasFeature() and rollback()..SH "QString QSqlDatabase::connectOptions () const"Returns the database connect options. See the "connectOptions" property for details..SH "bool QSqlDatabase::contains ( const QString & connectionName = defaultConnection )\fC [static]\fR"Returns TRUE if the list of database connections contains \fIconnectionName\fR; otherwise returns FALSE..SH "QSqlDatabase * QSqlDatabase::database ( const QString & connectionName = defaultConnection, bool open = TRUE )\fC [static]\fR"Returns the database connection called \fIconnectionName\fR. The database connection must have been previously added with addDatabase(). If \fIopen\fR is TRUE (the default) and the database connection is not already open it is opened now. If no \fIconnectionName\fR is specified the default connection is used. If \fIconnectionName\fR does not exist in the list of databases, 0 is returned. The pointer returned is owned by QSqlDatabase and should \fInot\fR be deleted..PPExamples:.)l sql/overview/basicbrowsing/main.cpp and sql/overview/create_connections/main.cpp..SH "QString QSqlDatabase::databaseName () const"Returns the name of the database. See the "databaseName" property for details..SH "QSqlDriver * QSqlDatabase::driver () const"Returns the database driver used to access the database connection..SH "QString QSqlDatabase::driverName () const"Returns the name of the driver used by the database connection..SH "QStringList QSqlDatabase::drivers ()\fC [static]\fR"Returns a list of all the available database drivers..PPNote that if you want to iterate over the list, you should iterate over a copy, e.g..PP.nf.br    QStringList list = QSqlDatabase::drivers();.br    QStringList::Iterator it = list.begin();.br    while( it != list.end() ) {.br        myProcessing( *it );.br        ++it;.br    }.br.fi.SH "QSqlQuery QSqlDatabase::exec ( const QString & query = QString::null ) const"Executes a SQL statement (e.g. an \fCINSERT\fR, \fCUPDATE\fR or \fCDELETE\fR statement) on the database, and returns a QSqlQuery object. Use lastError() to retrieve error information. If \fIquery\fR is QString::null, an empty, invalid query is returned and lastError() is not affected..PPSee also QSqlQuery and lastError()..SH "QString QSqlDatabase::hostName () const"Returns the host name where the database resides. See the "hostName" property for details..SH "bool QSqlDatabase::isDriverAvailable ( const QString & name )\fC [static]\fR"Returns TRUE if a driver called \fIname\fR is available; otherwise returns FALSE..PPSee also drivers()..SH "bool QSqlDatabase::isOpen () const"Returns TRUE if the database connection is currently open; otherwise returns FALSE..SH "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..SH "QSqlError QSqlDatabase::lastError () const"Returns information about the last error that occurred on the database. See QSqlError for more information.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -