Clementson's Blog

Bits and pieces (mostly Lisp-related) that I collect from the ether.

June 2004
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
May  Jul

Using MySql with Allegro CL

Wednesday, June 2, 2004

I've been playing around with the MySQL Direct Connect support in Allegro CL. MySql is the "world's most popular open source database" and can be used as either an open source product or as a commercial offering (which license you choose depends on how you intend to use the product), making it a flexible (and high performing) alternative to commercial database products. Although Franz has offered ODBC access to a range of databases in the past, the MySQL Direct Connect (first introduced as an update to ACL 6.2) connects directly to the MySQL server. This offers the following advantages:

To get some data to test it out, I exported the Customers table from the sample Northwind database in Microsoft Access (Note: I exported it as a tab-separated text file with no text qualifier and no field names on first row). Using ACL's MySql support to load the file and manipulate it was simple - it's just the same as using the MySql SQL commands:
CL-USER> (use-package :dbi.mysql)
; Autoloading for package "DBI.MYSQL":
; Fast loading c:binacl-7.0codeMYSQL.fasl
T
CL-USER> (connect :database "test")
; Fast loading from bundle codeacldns.fasl.
#
CL-USER> (sql "create table customers(CustomerID varchar(5), 
                                      CompanyName varchar(40), 
                                      ContactName varchar(30), 
                                      ContactTitle varchar(30), 
                                      Address varchar(60), 
                                      City varchar(15), 
                                      Region varchar(15), 
                                      PostalCode varchar(10), 
                                      Country varchar(15), 
                                      Phone varchar(24), 
                                      Fax varchar(24))")
NIL
CL-USER> (sql "load data infile 'c:/Customers.tab' into table customers")
NIL
CL-USER> (sql "select CustomerID, CompanyName, ContactName from customers limit 5")
(("ALFKI" "Alfreds Futterkiste" "Maria Anders")
 ("ANATR" "Ana Trujillo Emparedados y helados" "Ana Trujillo")
 ("ANTON" "Antonio Moreno Taquería" "Antonio Moreno")
 ("AROUT" "Around the Horn" "Thomas Hardy")
 ("BERGS" "Berglunds snabbköp" "Christina Berglund"))
("CustomerID" "CompanyName" "ContactName")
CL-USER> (sql-start "select CustomerID, CompanyName, ContactName from customers")
("CustomerID" "CompanyName" "ContactName")
CL-USER> (get-next-row)
("ALFKI" "Alfreds Futterkiste" "Maria Anders")
CL-USER> (get-next-row)
("ANATR" "Ana Trujillo Emparedados y helados" "Ana Trujillo")
CL-USER> (get-next-row)
("ANTON" "Antonio Moreno Taquería" "Antonio Moreno")
CL-USER> (with-db-rows ((CustomerID CompanyName) :table "customers") 
                (format t "Company ~s: ~s~%" CustomerID CompanyName))
Company "ALFKI": "Alfreds Futterkiste"
Company "ANATR": "Ana Trujillo Emparedados y helados"
Company "ANTON": "Antonio Moreno Taquería"
etc.
CL-USER> (disconnect)
NIL
CL-USER>

emacs Copyright © 2004 by Bill Clementson