|
Accessing External Database Engines |
Sheerpower can access data that is stored outside of Sheerpower. This lets a Sheerpower program read tables from external database engines such as Microsoft Access, Oracle, SQL Server, and any other systems that provide a Windows ODBC driver.
These database engines, cloud services, and file formats all provide Windows ODBC drivers, either directly from the vendor or through commonly used third-party ODBC drivers.
The connection is made through ODBC. ODBC stands for Open Database Connectivity. It is a standard Windows interface that lets a program talk to many different database engines using the same basic style of code.
Before Sheerpower can open an external database table, two things must already exist:
The ODBC driver knows how to communicate with the database engine. The data source tells Windows which database file or server to use. Once those are set up, Sheerpower can open the external table.
A Sheerpower program does not need a completely different style of code for every database engine. After the ODBC data source exists, the program opens the external table with an ordinary Sheerpower table statement.
For an external database table, the table name uses this form:
'table in datasource'
The first name is the table name inside the external database. The second name is the Windows ODBC data source name.
For example:
'contacts in mycontacts'
This means: open the Contacts table from the MyContacts ODBC data source.
The data source is created in Windows. This is normally done once for each external database that Sheerpower needs to access.
Sheerpower is a 64-bit program, so it can only see 64-bit data sources built with 64-bit ODBC drivers. Windows ships with two ODBC administrator programs, one for 32-bit drivers and one for 64-bit drivers. Always use the 64-bit one.
c:\sheerpower\samples\MyContacts.mdbAfter this setup is complete, Sheerpower can use the data source name in an open table statement.
The Microsoft Access driver is not always installed with Windows. If Microsoft Access Driver (*.mdb, *.accdb) does not appear in the driver list, download and install the 64-bit Microsoft Access Database Engine Redistributable from the Microsoft website, then repeat the steps above.
A User DSN is visible only to the Windows account that created it. If the Sheerpower program will run under a different account — for example, as a service or under SPINS — create the data source on the System DSN tab instead. The steps are the same.
The following program opens the Contacts table from the MyContacts ODBC data source. It extracts the records, sorts them by last name, and prints a contact list.
open table con: name 'contacts in mycontacts'
extract table con
sort by con(LastName)
end extract
print "Contact List"
print
for each con
print con(FirstName); ' '; con(LastName), &
con(MobilePhone), con(EmailName)
next con
close table con
end
This opens the external table and gives it the local table name
con. The phrase
'contacts in mycontacts' tells Sheerpower to open the
Contacts table from the MyContacts ODBC data source.
This creates the collection of records. In this example, all records are extracted.
This sorts the extracted records ascending by the LastName field.
This loops through the extracted records one record at a time.
This reads a field from the current record. For example,
con(FirstName) reads the FirstName field from the
current contact record.
This closes the table and releases the external table.
The output is a contact list sorted by last name:
Contact List
Steven Buchanan (206) 555-1856 [email protected]
Nancy Davolio (425) 555-9811 [email protected]
Andrew Fuller (206) 555-6666 [email protected]
External database engines can store data that is shared by many tools and applications. By using ODBC, Sheerpower can work with that data without requiring the data to be copied into a native Sheerpower table first.
This makes Sheerpower useful in environments where data already lives in an existing database system. The database engine stores and manages the data. Sheerpower opens the table, extracts records, sorts them, loops through them, and uses the fields just like a regular Sheerpower table.
To access an external database engine from Sheerpower, create an ODBC data source in Windows, then open the external table using this form:
open table handle: name 'table in datasource'
Once the table is open, the rest of the code uses familiar Sheerpower table statements: extract, sort, for each, field access, close, etc.
|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |