Qt-qodbc database programming-connect to MS access (1)

Source: Internet
Author: User
Qt provides a unified operation model similar to JDBC for database programming. It is a plug-in of a specific database or database interface at the underlying layer, and is responsible for real database operations.

Qt comes with a qodbc driver. On Windows, you can use the ODBC driver provided by the system to access databases that support ODBC, such as MS Access and SQL Server (ODBC driver with access and SQL Server in Windows XP ).

The basic class of the QT database model is the qsqldatabase object. A qsqldatabase object represents a database connection. On an open qsqldatabase object, we can perform various database operations.

The method for obtaining a qsqldatabase object is the static function adddatabase of qsqldatabase. It creates a new qsqldatabase object, registers it in an internal hash table, and finally returns this object. (In addition, the static function database can be used to query a registered qsqldatabase object)

When we get a new qsqldatabase object that is not open yet, we need to first set the database's DSN (Data Source Name). The DSN setting is through the member function setdatabasename, in qodbc, DSN can be in multiple forms --

1: name of the DSN registered by the operating system
2: An external DSN File
3: interpreted string

1 and 2 we can set or generate in the ODBC Management Panel of the Management panel, but there are no doubt 3rd forms are more flexible, and The DSN string connected to access has a similar form:

"Driver = {Microsoft Access Driver (*. mdb)}; fil = {MS access}; DBQ = xxx. mdb"

The following is a piece of code used to obtain the database connection of a specified MDB file. The function first checks whether the database connection of the file already exists. If yes, it checks whether the connection is enabled, if a connection is enabled, the system returns the result. If a connection is established but the connection is closed, the system returns the result. If no connection exists, a new connection is created. If yes, the system returns the result. A kdbexception will be thrown if opening fails.

Qsqldatabase kdbconnection: getaccessconnection (
Const qstring & accessfile, const qstring & username,
Const qstring & password)
...{
Const qstring prefix ("puremilk. Access. Connection ");

Qstring connid = prefix + "-" + accessfile;

Qsqldatabase connection = qsqldatabase: Database (connid, false );

If (connection. isvalid ())
...{
If (connection. isopen ())
Return connection;
}
Else
Connection = qsqldatabase: adddatabase ("qodbc", connid );

Qstring DSN = qstring (
"Driver = {Microsoft Access Driver (*. mdb)}; fil = {MS access}; DBQ = % 1"). Arg (accessfile );

Qdebug () <"Get Access database connection-" <DSN;

Connection. setdatabasename (DSN );

If (! Connection. Open (username, password ))
...{
Throw_exception (kdbexception, connection. lasterror (). Text ());
}

Return connection;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.