Sunday, December 5, 2010

Access database through ODBC

See how to setup and access an SQLite database through ODBC on Windows.

Recorded by Torsten Bergmann.

Download screencast (800x600): .mpg 39.4 MB, .mov 11.9 MB

View mobile version.



Load ODBC support:
Gofer new
 squeaksource: 'ODBC';
 package: 'ConfigurationOfODBC'; load.
(Smalltalk at: #ConfigurationOfODBC) load.

Download the ODBC driver for your database - here we use SQLite
Win32Shell new shellOpen: 'http://www.ch-werner.de/sqliteodbc/'

Configure an ODBC connection through Windows ODBC Administrator:
Win32Shell new 
 shellExecute: nil
 lpOperation: nil 
 lpFile: 'rundll32.exe' 
 lpParameters: 'shell32.dll,Control_RunDLL odbccp32.cpl' 
 lpDirectory: nil 
 nShowCmd: 5

Create database and fill some data:
|connection contents|
connection := ODBCConnection dsn: 'MySimpleDB' user: '' password: ''.
connection execute: 'CREATE TABLE user(name varchar(10), age smallint)'.
connection execute: 'INSERT INTO user VALUES(''Old Bob'', 100)'.
contents := (connection query: 'select * from user;') execute: ''.
contents asTable inspect.
connection close.

Reconnect and run the query again:
|connection contents|
connection := ODBCConnection dsn: 'MySimpleDB' user: '' password: ''.
contents := (connection query: 'select * from user;') execute: ''.
contents asTable inspect.
connection close.

2 comments:

  1. Win32Shell not working in Pharo1.4?
    I keep getting error codes from it, either 2 or 31.

    ReplyDelete
  2. fixed. Apparently my Pharo was... sick... heh...

    ReplyDelete