?? qsqldatabase.cpp
字號:
QStringList QSqlDatabase::drivers(){ QStringList list;#ifdef QT_SQL_PSQL list << QLatin1String("QPSQL7"); list << QLatin1String("QPSQL");#endif#ifdef QT_SQL_MYSQL list << QLatin1String("QMYSQL3"); list << QLatin1String("QMYSQL");#endif#ifdef QT_SQL_ODBC list << QLatin1String("QODBC3"); list << QLatin1String("QODBC");#endif#ifdef QT_SQL_OCI list << QLatin1String("QOCI8"); list << QLatin1String("QOCI");#endif#ifdef QT_SQL_TDS list << QLatin1String("QTDS7"); list << QLatin1String("QTDS");#endif#ifdef QT_SQL_DB2 list << QLatin1String("QDB2");#endif#ifdef QT_SQL_SQLITE list << QLatin1String("QSQLITE");#endif#ifdef QT_SQL_SQLITE2 list << QLatin1String("QSQLITE2");#endif#ifdef QT_SQL_IBASE list << QLatin1String("QIBASE");#endif#ifndef QT_NO_LIBRARY if (QFactoryLoader *fl = loader()) { QStringList keys = fl->keys(); for (QStringList::const_iterator i = keys.constBegin(); i != keys.constEnd(); ++i) { if (!list.contains(*i)) list << *i; } }#endif DriverDict dict = QSqlDatabasePrivate::driverDict(); for (DriverDict::const_iterator i = dict.constBegin(); i != dict.constEnd(); ++i) { if (!list.contains(i.key())) list << i.key(); } return list;}/*! This function registers a new SQL driver called \a name, within the SQL framework. This is useful if you have a custom SQL driver and don't want to compile it as a plugin. Example: \code QSqlDatabase::registerSqlDriver("MYDRIVER", new QSqlDriverCreator<MyDatabaseDriver>); QSqlDatabase db = QSqlDatabase::addDatabase("MYDRIVER"); \endcode QSqlDatabase takes ownership of the \a creator pointer, so you mustn't delete it yourself. \sa drivers()*/void QSqlDatabase::registerSqlDriver(const QString& name, QSqlDriverCreatorBase *creator){ delete QSqlDatabasePrivate::driverDict().take(name); if (creator) QSqlDatabasePrivate::driverDict().insert(name, creator);}/*! \threadsafe Returns true if the list of database connections contains \a connectionName; otherwise returns false. \sa connectionNames(), database()*/bool QSqlDatabase::contains(const QString& connectionName){ return QSqlDatabasePrivate::dbDict()->contains_ts(connectionName);}/*! \threadsafe Returns a list containing the names of all connections. \sa contains(), database()*/QStringList QSqlDatabase::connectionNames(){ return QSqlDatabasePrivate::dbDict()->keys_ts();}/*! \overload Creates a QSqlDatabase connection that uses the driver referred to by \a type. If the \a type is not recognized, the database connection will have no functionality. The currently available driver types are: \table \header \i Driver Type \i Description \row \i QDB2 \i IBM DB2 \row \i QIBASE \i Borland InterBase Driver \row \i QMYSQL \i MySQL Driver \row \i QOCI \i Oracle Call Interface Driver \row \i QODBC \i ODBC Driver (includes Microsoft SQL Server) \row \i QPSQL \i PostgreSQL Driver \row \i QSQLITE \i SQLite version 3 or above \row \i QSQLITE2 \i SQLite version 2 \row \i QTDS \i Sybase Adaptive Server \endtable Additional third party drivers, including your own custom drivers, can be loaded dynamically. \sa {SQL Database Drivers}, registerSqlDriver(), drivers()*/QSqlDatabase::QSqlDatabase(const QString &type){ d = new QSqlDatabasePrivate(); d->init(type);}/*! \overload Creates a database connection using the given \a driver.*/QSqlDatabase::QSqlDatabase(QSqlDriver *driver){ d = new QSqlDatabasePrivate(driver);}/*! Creates an empty, invalid QSqlDatabase object. Use addDatabase(), removeDatabase(), and database() to get valid QSqlDatabase objects.*/QSqlDatabase::QSqlDatabase(){ d = QSqlDatabasePrivate::shared_null(); d->ref.ref();}/*! Creates a copy of \a other.*/QSqlDatabase::QSqlDatabase(const QSqlDatabase &other){ d = other.d; d->ref.ref();}/*! Assigns \a other to this object.*/QSqlDatabase &QSqlDatabase::operator=(const QSqlDatabase &other){ qAtomicAssign(d, other.d); return *this;}/*! \internal Create the actual driver instance \a type.*/void QSqlDatabasePrivate::init(const QString &type){ drvName = type; if (!driver) {#ifdef QT_SQL_PSQL if (type == QLatin1String("QPSQL") || type == QLatin1String("QPSQL7")) driver = new QPSQLDriver();#endif#ifdef QT_SQL_MYSQL if (type == QLatin1String("QMYSQL") || type == QLatin1String("QMYSQL3")) driver = new QMYSQLDriver();#endif#ifdef QT_SQL_ODBC if (type == QLatin1String("QODBC") || type == QLatin1String("QODBC3")) driver = new QODBCDriver();#endif#ifdef QT_SQL_OCI if (type == QLatin1String("QOCI") || type == QLatin1String("QOCI8")) driver = new QOCIDriver();#endif#ifdef QT_SQL_TDS if (type == QLatin1String("QTDS") || type == QLatin1String("QTDS7")) driver = new QTDSDriver();#endif#ifdef QT_SQL_DB2 if (type == QLatin1String("QDB2")) driver = new QDB2Driver();#endif#ifdef QT_SQL_SQLITE if (type == QLatin1String("QSQLITE")) driver = new QSQLiteDriver();#endif#ifdef QT_SQL_SQLITE2 if (type == QLatin1String("QSQLITE2")) driver = new QSQLite2Driver();#endif#ifdef QT_SQL_IBASE if (type == QLatin1String("QIBASE")) driver = new QIBaseDriver();#endif } if (!driver) { DriverDict dict = QSqlDatabasePrivate::driverDict(); for (DriverDict::const_iterator it = dict.constBegin(); it != dict.constEnd() && !driver; ++it) { if (type == it.key()) { driver = ((QSqlDriverCreatorBase*)(*it))->createObject(); } } }#ifndef QT_NO_LIBRARY if (!driver && loader()) { if (QSqlDriverFactoryInterface *factory = qobject_cast<QSqlDriverFactoryInterface*>(loader()->instance(type))) driver = factory->create(type); }#endif // QT_NO_LIBRARY if (!driver) { qWarning("QSqlDatabase: %s driver not loaded", type.toLatin1().data()); qWarning("QSqlDatabase: available drivers: %s", QSqlDatabase::drivers().join(QLatin1String(" ")).toLatin1().data()); driver = shared_null()->driver; }}/*! Destroys the object and frees any allocated resources. If this is the last QSqlDatabase object that uses a certain database connection, the is automatically closed. \sa close()*/QSqlDatabase::~QSqlDatabase(){ if (!d->ref.deref()) { close(); delete d; }}/*! Executes a SQL statement on the database and returns a QSqlQuery object. Use lastError() to retrieve error information. If \a query is empty, an empty, invalid query is returned and lastError() is not affected. \sa QSqlQuery, lastError()*/QSqlQuery QSqlDatabase::exec(const QString & query) const{ QSqlQuery r(d->driver->createResult()); if (!query.isEmpty()) { r.exec(query); d->driver->setLastError(r.lastError()); } return r;}/*! Opens the database connection using the current connection values. Returns true on success; otherwise returns false. Error information can be retrieved using lastError(). \sa lastError() setDatabaseName() setUserName() setPassword() setHostName() setPort() setConnectOptions()*/bool QSqlDatabase::open(){ return d->driver->open(d->dbname, d->uname, d->pword, d->hname, d->port, d->connOptions);}/*! \overload Opens the database connection using the given \a user name and \a password. Returns true on success; otherwise returns false. Error information can be retrieved using the lastError() function. This function does not store the password it is given. Instead, the password is passed directly to the driver for opening the connection and it is then discarded. \sa lastError()*/bool QSqlDatabase::open(const QString& user, const QString& password){ setUserName(user); return d->driver->open(d->dbname, user, password, d->hname, d->port, d->connOptions);}/*! Closes the database connection, freeing any resources acquired. This will also affect copies of this QSqlDatabase object. \sa removeDatabase()*/void QSqlDatabase::close(){ d->driver->close();}/*! Returns true if the database connection is currently open; otherwise returns false.*/bool QSqlDatabase::isOpen() const{ return d->driver->isOpen();}/*! Returns true if there was an error opening the database connection; otherwise returns false. Error information can be retrieved using the lastError() function.*/bool QSqlDatabase::isOpenError() const{ return d->driver->isOpenError();}/*! Begins a transaction on the database if the driver supports transactions. Returns true if the operation succeeded; otherwise returns false. \sa QSqlDriver::hasFeature(), commit(), rollback()*/bool QSqlDatabase::transaction(){ if (!d->driver->hasFeature(QSqlDriver::Transactions)) return false; return d->driver->beginTransaction();}/*! Commits a transaction to the database if the driver supports transactions and a transaction() has been started. Returns true if the operation succeeded; otherwise returns false. Note that on some databases, this function will not work if there is an active QSqlQuery on the database. Use the lastError() function to retrieve database-specific error data about the error that occurred. \sa QSqlDriver::hasFeature() rollback()*/bool QSqlDatabase::commit(){ if (!d->driver->hasFeature(QSqlDriver::Transactions)) return false; return d->driver->commitTransaction();}/*! Rolls a transaction back on the database if the driver supports transactions and a transaction() has been started. Returns true if the operation succeeded; otherwise returns false. \sa QSqlDriver::hasFeature() commit()*/bool QSqlDatabase::rollback(){ if (!d->driver->hasFeature(QSqlDriver::Transactions)) return false; return d->driver->rollbackTransaction();}/*! Sets the connection's name to \a name. 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 name is database-specific. For the QOCI (Oracle) driver, the database name is the TNS Service Name. For the QODBC driver, the \a name can either be a DSN, a DSN filename (in which case the file must have a \c .dsn extension), or a connection string. For example, Microsoft Access users can use the following connection string to open an \c .mdb file directly, instead of having to create a DSN entry in the ODBC manager: \code ... db = QSqlDatabase::addDatabase("QODBC"); db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb"); if (db.open()) { // success! } ... \endcode There is no default value. \sa databaseName() setUserName() setPassword() setHostName() setPort() setConnectOptions() open()*/void QSqlDatabase::setDatabaseName(const QString& name){ if (isValid()) d->dbname = name;}/*! Sets the connection's user name to \a name. 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 userName() setDatabaseName() setPassword() setHostName() setPort() setConnectOptions() open()*/void QSqlDatabase::setUserName(const QString& name){ if (isValid()) d->uname = name;}/*! Sets the connection's password to \a password. 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. \warning This function stores the password in plain text within Qt. Use the open() call that takes a password as parameter to avoid this behavior. \sa password() setUserName() setDatabaseName() setHostName() setPort() setConnectOptions() open()*/void QSqlDatabase::setPassword(const QString& password){ if (isValid()) d->pword = password;}/*! Sets the connection's host name to \a host. This must be done before the connection is opened or it has no effect (or you can
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -