Skip to content

Instantly share code, notes, and snippets.



Created Nov 3, 2016
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)
insertCmd.ExecuteNonQuery() |> ignore
with _ -> ()
let connectDB () =
let conn = new SQLiteConnection("Data Source=Parts.sqlite3;Version=3;FailIfMissing=False")
createTable conn |> ignore
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
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
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.