Skip to content

Instantly share code, notes, and snippets.

@wezeku

wezeku/Program.fs

Created Nov 3, 2016
Embed
What would you like to do?
Quick 'n dirty F# SQLite sample
open System
open System.Data.SQLite
let createTable conn =
let cmdText =
"create table if not exists Part(
PartNo TEXT PRIMARY KEY not null,
PartName TEXT not null);"
use cmd = new SQLiteCommand(cmdText, conn)
cmd.ExecuteNonQuery() |> ignore
// add some test data
let insertText =
"insert into Part (PartNo, PartName)
values ('GSKT', 'Gasket'),
('FPMP', 'Fuel pump'),
('TRMO', 'Thermostat'),
('GNRT', 'Generator');"
use insertCmd = new SQLiteCommand(insertText, conn)
try
insertCmd.ExecuteNonQuery() |> ignore
with _ -> ()
let connectDB () =
let conn = new SQLiteConnection("Data Source=Parts.sqlite3;Version=3;FailIfMissing=False")
conn.Open()
createTable conn |> ignore
conn
let getParts dbConn =
let cmdText =
"select PartNo, PartName from Part order by PartNo;"
use cmd = new SQLiteCommand(cmdText, dbConn)
use reader = cmd.ExecuteReader()
seq { while reader.Read() do yield (reader.GetString 0, reader.GetString 1) }
|> Seq.toList
[<EntryPoint>]
let main argv =
use dbConn = connectDB ()
printfn "SQLite version: %s" dbConn.ServerVersion
printfn "Bitness: %d\n" (IntPtr.Size*8)
printfn "PartNo PartName"
printfn "---------- -------------------"
for (partNo, partName) in getParts dbConn do
printfn "%-10s %s" partNo partName
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.