?? qsqldatabase.cpp
字號:
close() the connection, call this function and open() the connection again). There is no default value. \sa hostName() setUserName() setPassword() setDatabaseName() setPort() setConnectOptions() open()*/void QSqlDatabase::setHostName(const QString& host){ if (isValid()) d->hname = host;}/*! Sets the connection's port number to \a port. This must be done before the connection is opened or it has no effect (or you can close() the connection, call this function and open() the connection again). There is no default value. \sa port() setUserName() setPassword() setHostName() setDatabaseName() setConnectOptions() open()*/void QSqlDatabase::setPort(int port){ if (isValid()) d->port = port;}/*! Returns the connection's database name; it may be empty. \sa setDatabaseName()*/QString QSqlDatabase::databaseName() const{ return d->dbname;}/*! Returns the connection's user name; it may be empty. \sa setUserName()*/QString QSqlDatabase::userName() const{ return d->uname;}/*! Returns the connection's password. If the password was not set with setPassword(), and if the password was given in the open() call, or if no password was used, an empty string is returned.*/QString QSqlDatabase::password() const{ return d->pword;}/*! Returns the connection's host name. It may be empty. \sa setHostName()*/QString QSqlDatabase::hostName() const{ return d->hname;}/*! Returns the connection's driver name. \sa addDatabase(), driver()*/QString QSqlDatabase::driverName() const{ return d->drvName;}/*! Returns the connection's port number. The value is undefined if the port number has not been set. \sa setPort()*/int QSqlDatabase::port() const{ return d->port;}/*! Returns the database driver used to access the database connection. \sa addDatabase() drivers()*/QSqlDriver* QSqlDatabase::driver() const{ return d->driver;}/*! Returns information about the last error that occurred on the database. Failures that occur in conjunction with an individual query are reported by QSqlQuery::lastError(). \sa QSqlError, QSqlQuery::lastError()*/QSqlError QSqlDatabase::lastError() const{ return d->driver->lastError();}/*! Returns a list of the database's tables, system tables and views, as specified by the parameter \a type. \sa primaryIndex(), record()*/QStringList QSqlDatabase::tables(QSql::TableType type) const{ return d->driver->tables(type);}/*! Returns the primary index for table \a tablename. If no primary index exists an empty QSqlIndex is returned. \sa tables(), record()*/QSqlIndex QSqlDatabase::primaryIndex(const QString& tablename) const{ return d->driver->primaryIndex(tablename);}/*! Returns a QSqlRecord populated with the names of all the fields in the table (or view) called \a tablename. The order in which the fields appear in the record is undefined. If no such table (or view) exists, an empty record is returned.*/QSqlRecord QSqlDatabase::record(const QString& tablename) const{ return d->driver->record(tablename);}/*! Sets database-specific \a options. This must be done before the connection is opened or it has no effect (or you can close() the connection, call this function and open() the connection again). The format of the \a options string is a semicolon separated list of option names or option=value pairs. The options depend on the database client used: \table \header \i ODBC \i MySQL \i PostgreSQL \row \i \list \i SQL_ATTR_ACCESS_MODE \i SQL_ATTR_LOGIN_TIMEOUT \i SQL_ATTR_CONNECTION_TIMEOUT \i SQL_ATTR_CURRENT_CATALOG \i SQL_ATTR_METADATA_ID \i SQL_ATTR_PACKET_SIZE \i SQL_ATTR_TRACEFILE \i SQL_ATTR_TRACE \i SQL_ATTR_CONNECTION_POOLING \endlist \i \list \i CLIENT_COMPRESS \i CLIENT_FOUND_ROWS \i CLIENT_IGNORE_SPACE \i CLIENT_SSL \i CLIENT_ODBC \i CLIENT_NO_SCHEMA \i CLIENT_INTERACTIVE \i UNIX_SOCKET \endlist \i \list \i connect_timeout \i options \i tty \i requiressl \i service \endlist \header \i DB2 \i OCI \i TDS \row \i \list \i SQL_ATTR_ACCESS_MODE \i SQL_ATTR_LOGIN_TIMEOUT \endlist \i \list \i OCI_ATTR_PREFETCH_ROWS \i OCI_ATTR_PREFETCH_MEMORY \endlist \i \e none \header \i SQLite \row \i \list \i QSQLITE_BUSY_TIMEOUT \endlist \endtable Examples: \code ... // MySQL connection db.setConnectOptions("CLIENT_SSL=1;CLIENT_IGNORE_SPACE=1"); // use an SSL connection to the server if (!db.open()) { db.setConnectOptions(); // clears the connect option string ... } ... // PostgreSQL connection db.setConnectOptions("requiressl=1"); // enable PostgreSQL SSL connections if (!db.open()) { db.setConnectOptions(); // clear options ... } ... // ODBC connection db.setConnectOptions("SQL_ATTR_ACCESS_MODE=SQL_MODE_READ_ONLY;SQL_ATTR_TRACE=SQL_OPT_TRACE_ON"); // set ODBC options if (!db.open()) { db.setConnectOptions(); // don't try to set this option ... } \endcode Refer to the client library documentation for more information about the different options. \sa connectOptions()*/void QSqlDatabase::setConnectOptions(const QString &options){ if (isValid()) d->connOptions = options;}/*! Returns the connection options string used for this connection. The string may be empty. \sa setConnectOptions() */QString QSqlDatabase::connectOptions() const{ return d->connOptions;}/*! Returns true if a driver called \a name is available; otherwise returns false. \sa drivers()*/bool QSqlDatabase::isDriverAvailable(const QString& name){ return drivers().contains(name);}/*! \overload This 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 QPSQL driver can be done like this: \code #include "qtdir/src/sql/drivers/psql/qsql_psql.cpp" \endcode (We assume that \c qtdir 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. \code PGconn *con = PQconnectdb("host=server user=bart password=simpson dbname=springfield"); QPSQLDriver *drv = new QPSQLDriver(con); QSqlDatabase db = QSqlDatabase::addDatabase(drv); // becomes the new default connection QSqlQuery query; query.exec("SELECT NAME, ID FROM STAFF"); ... \endcode The 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. Remember that you must link your application against the database client library as well. The simplest way to do this is to add lines like the ones below to your \c .pro file: \code unix:LIBS += -lpq win32:LIBS += libpqdll.lib \endcode You will need to have the client library in your linker's search path. The 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. \table \header \i Driver \i Class name \i Constructor arguments \i File to include \row \i QPSQL \i QPSQLDriver \i PGconn *connection \i \c qsql_psql.cpp \row \i QMYSQL \i QMYSQLDriver \i MYSQL *connection \i \c qsql_mysql.cpp \row \i QOCI \i QOCIDriver \i OCIEnv *environment, OCISvcCtx *serviceContext \i \c qsql_oci.cpp \row \i QODBC \i QODBCDriver \i SQLHANDLE environment, SQLHANDLE connection \i \c qsql_odbc.cpp \row \i QDB2 \i QDB2 \i SQLHANDLE environment, SQLHANDLE connection \i \c qsql_db2.cpp \row \i QTDS \i QTDSDriver \i LOGINREC *loginRecord, DBPROCESS *dbProcess, const QString &hostName \i \c qsql_tds.cpp \row \i QSQLITE \i QSQLiteDriver \i sqlite *connection \i \c qsql_sqlite.cpp \row \i QIBASE \i QIBaseDriver \i isc_db_handle connection \i \c qsql_ibase.cpp \endtable 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/\l{QSqlCursor} objects from blocking each other. \warning If you add a database with the same name as an existing database, the new database will replace the old one. \warning The SQL framework takes ownership of the \a driver pointer, and it should not be deleted. If you want to explicitly remove the connection, use removeDatabase(). \sa drivers()*/QSqlDatabase QSqlDatabase::addDatabase(QSqlDriver* driver, const QString& connectionName){ QSqlDatabase db(driver); QSqlDatabasePrivate::addDatabase(db, connectionName); return db;}/*! Returns true if the QSqlDatabase has a valid driver. Example: \code QSqlDatabase db; qDebug() << db.isValid(); // Returns false db = QSqlDatabase::database("sales"); qDebug() << db.isValid(); // Returns true if "sales" connection exists QSqlDatabase::removeDatabase("sales"); qDebug() << db.isValid(); // Returns false \endcode*/bool QSqlDatabase::isValid() const{ return d->driver && d->driver != d->shared_null()->driver;}#ifdef QT3_SUPPORT/*! Use query.record() instead.*/QSqlRecord QSqlDatabase::record(const QSqlQuery& query) const{ return query.record(); }/*! Use query.record() instead.*/QSqlRecord QSqlDatabase::recordInfo(const QSqlQuery& query) const{ return query.record(); }/*! \fn QSqlRecord QSqlDatabase::recordInfo(const QString& tablename) const Use record() instead.*/#endif/*! Clones the database connection \a other and and stores it as \a connectionName. All the settings from the original database, e.g. databaseName(), hostName(), etc., are copied across. Does nothing if \a other is an invalid database. Returns the newly created database connection. Note that the connection is not opened, to use it, it is necessary to call open() first.*/QSqlDatabase QSqlDatabase::cloneDatabase(const QSqlDatabase &other, const QString &connectionName){ if (!other.isValid()) return QSqlDatabase(); QSqlDatabase db(other.driverName()); db.d->copy(other.d); QSqlDatabasePrivate::addDatabase(db, connectionName); return db;}#ifndef QT_NO_DEBUG_STREAMQDebug operator<<(QDebug dbg, const QSqlDatabase &d){ if (!d.isValid()) { dbg.nospace() << "QSqlDatabase(invalid)"; return dbg.space(); } dbg.nospace() << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\"" << d.databaseName() << "\", host=\"" << d.hostName() << "\", port=" << d.port() << ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ")"; return dbg.space();}#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -