Example
Assume that the data source ``mydata'' defines a database that contains a table ``pricelist'' of the following form:
articlenum | color | price |
1001 | blue | 10.49 |
1002 | red | 10.49 |
1003 | black | 5.99 |
1004 | blue | 3.99 |
... |
The following small example shows how to connect to a database from an Mosel model file, read in data, and disconnect from the data source.
model 'ODBCexample' uses 'mmodbc' declarations prices: array (range) of real end-declarations setparam("SQLverbose", true) ! Enable ODBC message printing in case of error SQLconnect("DSN=mydata") ! Connect to the database defined by `mydata' writeln("Connection number: ", getparam("SQLconnection")) SQLexecute("select articlenum,price from pricelist", prices) ! Get the entries of field `price' (indexed by ! field `articlenum') in table `pricelist' SQLdisconnect ! Disconnect from the database end-model
Here the SQLverbose control parameter is set to true to enable ODBC message printing in case of error. Following the connection, the procedure SQLexecute is called to retrieve entries from the field price (indexed by field articlenum) in the table pricelist. Finally, the connection is closed.
For further examples of working with databases and spreadsheets, the reader is referred to the Xpress whitepaper Using ODBC and other database interfaces with Mosel.