Guest User

Untitled

a guest
Jan 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. module DatabaseTest where
  2.  
  3. import Database.HDBC
  4. import Database.HDBC.MySQL
  5. -- cabal install MissingH
  6. import Data.List.Utils
  7.  
  8. startDb = connectMySQL defaultMySQLConnectInfo {
  9. mysqlHost = "localhost",
  10. mysqlDatabase = "movies",
  11. mysqlUser = "root",
  12. mysqlPassword = "5texmex2go",
  13. mysqlUnixSocket = "/var/run/mysqld/mysqld.sock"
  14. }
  15.  
  16. addMovie conn title year = do
  17. let params = join "," ["title", "year"]
  18. run conn ("INSERT INTO movies_movie (" ++ params ++ ") VALUES (?, ?)")
  19. [toSql title, nToSql year]
  20. commit conn
  21.  
  22.  
  23. {-
  24. Why is this only hitting the first result?
  25. [("id",SqlInt32 1),("title",SqlByteString "testing"),("year",SqlInt32 0)]
  26. [("id",SqlInt32 2),("title",SqlByteString "testing 123"),("year",SqlInt32 0)]
  27. [("id",SqlInt32 3),("title",SqlByteString "testing '123"),("year",SqlInt32 0)]
  28. [("id",SqlInt32 4),("title",SqlByteString "testing '123"),("year",SqlInt32 0)]
  29. [("id",SqlInt32 5),("title",SqlByteString "testing '123"),("year",SqlInt32 1999)]
  30. -}
  31. printMovie results = case results of
  32. row@_ -> mapM_ printMovieData row
  33.  
  34. -- [("id",SqlInt32 1),("title",SqlByteString "testing"),("year",SqlInt32 0)]
  35. printMovieData row = case row of
  36. id@("id", _) -> print (snd id)
  37. title@("title", _) -> print (snd title)
  38.  
  39. main = do
  40. conn <- startDb
  41.  
  42. -- This is awesome I don't have to type a bunch of (),; crap!
  43. -- addMovie conn "testing '123" 1999
  44.  
  45. stmt <- prepare conn "SELECT * FROM movies_movie WHERE 1"
  46. execute stmt []
  47. results <- fetchAllRowsAL stmt
  48. mapM_ printMovie results
  49.  
  50. disconnect conn
  51.  
  52. print "Done!"
Add Comment
Please, Sign In to add comment