Created
February 29, 2016 18:49
-
-
Save jackfoxy/50ffcefb88f082c51bb1 to your computer and use it in GitHub Desktop.
SQLite cannot execute from F# FSI
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
//Demo SQLite cannot run from FSI | |
//#load "load-references-debug.fsx" | |
#I __SOURCE_DIRECTORY__ | |
#r "../../packages/Microsoft.Data.Sqlite.1.0.0-rc1-final/lib/net451/Microsoft.Data.Sqlite.dll" | |
open Microsoft.Data.Sqlite | |
let conn = new SqliteConnection(@"Data Source = :memory:") | |
conn.Open() | |
let createTable = "CREATE TABLE foo (a int, b int)" | |
let cmd = new SqliteCommand(createTable, conn) | |
let rc = cmd.ExecuteNonQuery() | |
// TWO: insert 16 rows | |
for i = 1 to 16 do | |
let sql = (sprintf "INSERT INTO foo (a) VALUES (%d)" i) | |
cmd.CommandText <- sql | |
cmd.ExecuteNonQuery() |> ignore | |
cmd.CommandText <- "UPDATE foo SET b = a * a" | |
cmd.ExecuteNonQuery() |> ignore | |
cmd.CommandText <- "SELECT b FROM foo WHERE a=7" | |
let rdr = cmd.ExecuteReader() | |
rdr.Read() |> ignore | |
let vsq = rdr.GetInt32(0) | |
rdr.Close() | |
rdr.Dispose() | |
let mutable vsum = 0 | |
cmd.CommandText <- "SELECT a FROM foo WHERE b<120" | |
let rdr2 = cmd.ExecuteReader() | |
while rdr2.Read() do | |
vsum <- vsum + rdr2.GetInt32(0) | |
rdr2.Close() | |
rdr2.Dispose() | |
cmd.Dispose() | |
conn.Dispose() | |
printfn "val: %d" vsq | |
printfn "sum: %d" vsum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment