[Haskell-cafe] How to program with sqlite?

Bjorn Bringert bjorn at bringert.net
Tue Mar 25 10:24:26 EDT 2008


2008/3/22 Sebastian Sylvan <sebastian.sylvan at gmail.com>:
>
> On Sat, Mar 22, 2008 at 1:40 PM, Deng Chao <knifewolf at gmail.com> wrote:
> > Hi all,
> >  I'm learning sqlite, and as I know haskell has some libraries like
> > HDBC or HSQL can access sqlite DB. Can anybody give me a small example
> > to show how to use it? It will be very appreciate? Thanks!
> >
> > Best Regards,
> > Deng Chao
> >
> Here's a quick GHCi session with HDBC.
>
> Prelude> :m +Database.HDBC
> Prelude Database.HDBC> :m +Database.HDBC.Sqlite3
> Prelude Database.HDBC Database.HDBC.Sqlite3> conn <- connectSqlite3 "mydb"
>  Prelude Database.HDBC Database.HDBC.Sqlite3> quickQuery conn "CREATE TABLE
> mytable (FirstName varchar, LastName varchar, Age int )" []
> []
> Prelude Database.HDBC Database.HDBC.Sqlite3> quickQuery conn "INSERT INTO
> mytable VALUES ('Sebastian','Sylvan',26)" []
>  []
> Prelude Database.HDBC Database.HDBC.Sqlite3> commit conn
> Prelude Database.HDBC Database.HDBC.Sqlite3> quickQuery conn "SELECT * FROM
> mytable" []
> [[SqlString "Sebastian",SqlString "Sylvan",SqlString "26"]]
>  Prelude Database.HDBC Database.HDBC.Sqlite3> disconnect conn
>
>
> Not sure why that Age field came back as a string though :-)

This is SQLite's fault. In SQLite, all types are aliases for STRING.
Whatever type you use in the create and insert, you will get strings
back.

/Björn


More information about the Haskell-Cafe mailing list