Table of Contents

1 HDBC-sqlite3

cabal install HDBC-sqlite3 
cabal install sqlite
-- run ghci
ghci

-- load module
ghci>:module Database.HDBC Database.HDBC.Sqlite3

-- create connection
ghci>conn <- connectSqlite3 "students.db"
-- check the type of connection 
ghci>:t conn

-- check the function
ghci>:t connectSqlite3

-- create a table
ghci>run conn "CREATE TABLE names (id INTEGER NOT NULL, fname VARCHAR(80), lname VARCHAR(80))" []

-- check the type of signature of run
ghci>:run

-- insert data to table
ghci>run conn "INSERT INTO names (id, fname, lname) VALUES(1, 'Edwin', 'Brady')" []

-- commit it
ghci>commit conn

-- query data from table
ghci> quickQuery conn "SELECT * from names" []

-- login to sqlite3 in Command Line if you want to
sqlite3 students.db

2 sqlite-simple

  • Install sqlite-simple.
cabal install sqlite-simple
  • Local file example.
/Users/cat/myfile/bitbucket/haskell/Sqlite.hs
http://hackage.haskell.org/package/sqlite-simple
  • Run sqlite from command line with database file.
# from my MacOS
sqlite3  /Users/cat/myfile/bitbucket/testfile/test.db
  • Sqlit console commands, or sqlit3 -help
# quit
.quit
# show tables
.tables
  • Open connection in Haskell.
conn <- open "test.db"

3 Haskell and Sqlite Commands

  • Empty table
  • Create table
  • Insert data
  • Select data from table

Author: cat

Created: 2019-07-09 Tue 16:58

Validate