2 people like it.

FsSql usage example

FsSql is originally here: https://github.com/mausch/FsSql and better sample is there too but I just want to share it.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
type SQLite() =
    let dbexist = File.Exists(NSettings.dbpath)
    let connSetting = sprintf "Data Source=%s;Version=3;New=%s;Compress=True;" 
                        <| NSettings.dbpath 
                        <| if (dbexist) then "False" else "True"
    let openConn() =
        let conn = new SQLiteConnection(connSetting)
        conn.Open()
        conn :> IDbConnection
    let connMgr = Sql.withNewConnection openConn
    let exec a = Sql.execNonQuery(connMgr) a [] |> ignore
    let P = Sql.Parameter.make
    let getNode (dr: IDataRecord) =
        let date, value =   (dr?date).Value 
                            (dr?value).Value
        new dbNode(date, value)
    do
        if not dbexist then
            exec "CREATE TABLE \"archive\" ( \"date\" INTEGER, \"value\" INTEGER)"
            exec "CREATE TABLE \"instant\" ( \"date\" INTEGER, \"value\" INTEGER)"
    member X.execute q = exec q
    member X.query q =
        Sql.execReader(connMgr) q []
        |> Seq.ofDataReader
        |> Seq.map getNode
        |> Seq.toArray
Multiple items
type SQLite =
  new : unit -> SQLite
  member execute : q:'c -> unit
  member query : q:'a -> 'b []

Full name: Script.SQLite

--------------------
new : unit -> SQLite
val dbexist : bool
val connSetting : string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val openConn : (unit -> 'a)
val conn : obj
val connMgr : obj
val exec : ('a -> unit)
val a : 'a
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val P : obj
val getNode : (obj -> 'a)
val dr : obj
val date : obj
val value : obj
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
val X : SQLite
member SQLite.execute : q:'c -> unit

Full name: Script.SQLite.execute
val q : 'c
member SQLite.query : q:'a -> 'b []

Full name: Script.SQLite.query
val q : 'a
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
Raw view Test code New version

More information

Link:http://fssnip.net/eX
Posted:11 years ago
Author:Ash Harley
Tags: sql , sqlite , f# , database