How to connect Database using ODBC
This lesson demonstrates how to use ODBC to connect to a database. Here we have accessed MS Access Database and have fetched results from it. We have created this example using LiveCode, MS Access 2003 and Windows XP, but you can use ODBC to connect to any database that supports it, from within LiveCode.
You can download the associated sample stack for this lesson here.
Creating a Database using MS Access
Creating a table in MS Access
Click "Create Table in Design View" , this will open up the table design. Add two fields labeled Name and Company to the table and save it.
Fill the table with Data
Double click the table "test", this will open the blank table. Fill in some data and then save the table and database.
Create a ODBC connection for the Database livecodeTest
Step 1: Goto Control panel -> Administrative Tools -> Data Sources(ODBC).
Step 2: Add new Data Source
Step 3: Selecting the Database file and Naming it.
Step 4: Creating Login and Password
Step 5: Accessing ODBC connection in LiveCode
Step 6: Coding for Fetch Data
--This will open up connection for the MS Access Database using a ODBC connection
put revOpenDatabase("ODBC", "runrev", "livecodeTest", "testUser", "123456" ) into tDatabaseID
if tDatabaseID is null then
answer "Not connected"
--This statemenr will select all the data from the table Test
put "SELECT * FROM test" into tQuery
--This will query the Database Table using the statement in tQuery
put revDataFromQuery(tab, cr, tDatabaseID, tQuery) into tData
--This will display all the data from the Database
put tData into field 1
--This closes the Database connection
Step 7: Displaying Results
Click on the Fetch Data button, and you should see a result similar to the above. We have fetched the data to LiveCode from a MS Access Database using the ODBC connection.
Note: While accessing the MS Access Database file from LiveCode, you need to make sure the Access Database is closed, otherwise it will throw an error.