ODBCDatabase

From Xojo Documentation

Class (inherits from Database)

Used to open an ODBC database using an ODBC drivers. You use the constants in the ODBCConstant module with the methods and properties of the ODBCDatabase class.

Properties
Attribute DatabaseName ScrollableCursor
AttributeString ExtendedSchema Timeout
DBMS fa-lock-32.png Host UserName
DataSource Password
Methods
AddRow DataTypeInfo RollbackTransaction
BeginTransaction DriverNames SelectSQL
Close ExecuteSQL SpecialColumns
ColumnPrivileges ForeignKeys TableColumns
CommitTransaction NextRowSet TableIndexes
Connect Prepare TablePrivileges
ConnectionAttribute PrimaryKeys Tables
ConnectionInfo ProcedureColumns
DataSourceNames Procedures

Notes

In order to use this class, you must have the ODBCDatabase plug-in in your plugins folder.

The ODBCDatabase class also requires a driver manager and driver. On the Windows platform, the driver manager is supplied by Microsoft as part of Windows or as part of the MDAC package. For non-Windows platforms, the iODBC driver manager is required, which is available from http://www.iodbc.org or is installed with most commercial drivers.

ODBC drivers may be included with the OS, or may be obtained from the database vendor or a commercial developer of ODBC drivers. Non-Windows drivers must be compatible with the iODBC standard. For macOS, Actual Technologies supplies iODBC compliant drivers that work well with Xojo apps. There are other vendors as well.

The driver must match the architecture of the app. So a 64-bit app must use a 64-bit ODBC driver and a 32-bit app must use a 32-bit ODBC driver.

The ODBCDatabase engine supports all four RowSet navigation methods: MoveFirst, MoveNext, MovePrevious, and MoveLast.

PostgreSQL Notes

On macOS, when connecting to PostgreSQL using ODBC you must specify a username and password. If you do not, the ODBC driver will crash your app.

Threading

SelectSQL and ExecuteSQL statements do not block when called from within Threads.

Sample Code

This code prompts the user to select a Data Source from either the User or System data sources:

Var db As New ODBCDatabase
db.DataSource = ""
Try
db.Connect
// proceed with database operations
Catch error As DatabaseExcecption
MessageBox("Error: " + error.ErrorMessage)
End Try

This code specifies a specific data source to use:

Var db As New ODBCDatabase
db.DataSource = "TestAccessDB"
Try
db.Connect
// proceed with database operations
Catch error As DatabaseExcecption
MessageBox("Error: " + error.ErrorMessage)
End Try

This code specifies the DSN information to connect to a Microsoft Access database:

Var db As New ODBCDatabase
db.DataSource = "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\TestAccessDB.mdb;Uid=Admin;Pwd=;"
Try
db.Connect
// proceed with database operations
Catch error As DatabaseExcecption
MessageBox("Error: " + error.ErrorMessage)
End Try

See Also

Database Class, DatabaseColumn, DatabaseRow, ODBCPreparedStatement, RowSet classes.