There are two predicates for connecting to databases, db_connect/5 and db_connect/6. The db_connect/5 predicate is for ODBC connections, while db_connect/6 is for other (non-ODBC) database drivers.
| ?- db_connect(+Handle, +Driver, +DSN, +User, +Password). | ?- db_connect(+Handle, +Driver, +Server, +Database, +User, +Password).
The db_connect/5 predicate assumes that an entry for a data source name (DSN) exists in the odbc.ini file. The Handle is the handle name used for the connection. The Driver is the driver being used for the connection. The User and Password are the user name and password being used for the connection. The user is responsible for giving the name to the handle. To connect to the data source mydb using the user name xsb and password xsb with the odbc driver, the call is as follows:
| ?- db_connect(ha, odbc, mydb, xsb, xsb).
where ha is the user-chosen handle name (a Prolog atom) for the connection.
The db_connect/6 predicate is used for drivers other than ODBC. The arguments Handle, Driver, User, and Password are the same as for db_connect/5. The Server and Database arguments specify the server and database to connect to. For example, for a connection to a database called test located on the server wolfe with the user name xsb, the password foo, and using the mysql driver, the call is:
| ?- db_connect(ha, mysql, wolfe, test, xsb, foo).
where ha is the handle name the user chose for the connection.
If the connection is successfully made, the predicate invocation will succeed. This step is necessary before anything can be done with the data sources since it gives XSB the opportunity to initialize system resources for the session.
To close a database connection use:
| ?- db_disconnect(Handle).
where handle is the handle name for the connection. For example, to close the connection to above mysql database call:
| ?- db_disconnect(ha).
and XSB will give all the resources it allocated for this session back to the system.