Accessing Data with ODBC
ODBC provides a uniform API to access all relational data sources. Because ODBC has broad support from application and database vendors, the result is a single API that provides all the functionality that application developers need. This uniform data access architecture ensures interoperability and a common approach to data access for the many differing relational data sources.
An application uses the following function calls and processes to access a data source using the ODBC API.
-
Allocate environment handle Identifies the memory location for global data and status information for the defined connections.
-
Allocate connection Identifies the memory location for data about a particular connection.
-
Connect Specifies the connection authorization information (such as data source name, user identification, and password).
-
Allocate statement Associates an SQL statement with a connection. Many different SQL statements can be associated with a connection, but only one at a time.
-
Execute SQL statement Processes the SQL statement using the database engine.
-
Fetch result set Receives the results of the SQL statement (such as all rows, or just the first, last, next, or previous row), and also gets information about the results (such as number of rows or number of columns).
-
Free statement Drops the statement from the connection. You can now associate some other SQL statement with the same connection.
-
Disconnect Removes the data source name and authorization information from the connection.
-
Free connection Drops the connection.
-
Free environment handle Drops the global data and frees all associated memory.
By coding to the ODBC API, you can create database-independent code that automatically adapts to a variety of databases. However, there is one important consideration in adopting this approach. While any specific ODBC driver can leverage unique data source functions, the same functions might not be supported by other drivers. If your application is designed to be used across a variety of databases, these extended functions should be used carefully or not at all.