Created
November 3, 2016 17:53
-
-
Save wezeku/9120e80bd6cb181f195c09c761d20647 to your computer and use it in GitHub Desktop.
Quick 'n dirty F# SQLite sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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