使用Haskell中的参数进行Sqlite3更新



最近我开始在haskell中使用sqlite3,我想知道如何用参数作为偏移量修改表中的变量?

我想做这样的事情:

execute_ conn "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, v INTEGER)"
execute conn "INSERT INTO test (v) VALUES (?)" (Only ("1" :: String))
execute conn "UPDATE test SET v = v + ?" (Only modifyingValue) <--- this is not working

谢谢。

以下是关于ErrorMisuse错误的信息:

https://www.sqlite.org/cvstrac/wiki?p=LibraryRoutineCalledOutOfSequence

也许连接已经关闭?还是在两个不同的线程中使用相同的连接?

以下是一些有效的示例代码:

{-# LANGUAGE OverloadedStrings #-}
import           Control.Applicative
import qualified Data.Text as T
import           Database.SQLite.Simple
import           Database.SQLite.Simple.FromRow
dumpTable conn table = do
  r <- query_ conn "SELECT * from test" :: IO [(Int,String,Int)]
  mapM_ print r
main :: IO ()
main = do
  conn <- open "test.db"
  execute_ conn "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, firstname TEXT, pets INTEGER)"
  execute conn "INSERT INTO test (firstname, pets) VALUES (?,?)"
               ("Alice" :: String, 2 :: Int)
  execute conn "INSERT INTO test (firstname, pets) VALUES (?,?)"
               ("Bob" :: String, 3 :: Int) 
  putStrLn "before:"
  dumpTable conn "test"
  execute conn "UPDATE TEST SET pets = pets + ?" (Only (4 :: Int))
  putStrLn "after:"
  dumpTable conn "test"
  close conn

最新更新