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

时间:2016-05-16 17:09:54

标签: database haskell sqlite sql-update

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

我想做这样的事情:

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

谢谢。

1 个答案:

答案 0 :(得分:0)

以下是有关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