Sqlite3 - INSERT statement question

Good morning, I'm using "Real World Haskell" and a Windows Vista PC and I have Sqlite3 installed...[note: I had to change test1.db to /users/user/test1.db in order to get this to work, otherwise, neither the database nor the table could be created -- also, note c:/users/user/test1.db gives a syntax error], ghci doesn't like c:/]: I tried to use the following example from the book but it failed...see following: Prelude> :m Database.HDBC Database.HDBC.Sqlite3 Prelude Database.HDBC Database.HDBC.Sqlite3> conn <- connectSqlite3 "/users/user/test1.db" ... Prelude Database.HDBC Database.HDBC.Sqlite3> run conn "INSERT INTO test VALUES (?, ?)" [toSql 0, toSql "zero"] <interactive>:1:43: No instance for (Data.Convertible.Base.Convertible t SqlValue) arising from a use of `toSql' at <interactive>:1:43-49 Possible fix: add an instance declaration for (Data.Convertible.Base.Convertible t SqlValue) In the expression: toSql 0 In the third argument of `run', namely `[toSql 0, toSql "zero"]' In the expression: run conn "INSERT INTO test VALUES (?, ?)" [toSql 0, toSql "zero"] I'd appreciate any advice...

On Thu, Feb 03, 2011 at 12:20:12PM -0500, Patrick Lynch wrote:
Good morning,
I'm using "Real World Haskell" and a Windows Vista PC and I have Sqlite3 installed...[note: I had to change test1.db to /users/user/test1.db in order to get this to work, otherwise, neither the database nor the table could be created -- also, note c:/users/user/test1.db gives a syntax error], ghci doesn't like c:/]:
I tried to use the following example from the book but it failed...see following:
Prelude> :m Database.HDBC Database.HDBC.Sqlite3 Prelude Database.HDBC Database.HDBC.Sqlite3> conn <- connectSqlite3 "/users/user/test1.db" ... Prelude Database.HDBC Database.HDBC.Sqlite3> run conn "INSERT INTO test VALUES (?, ?)" [toSql 0, toSql "zero"]
<interactive>:1:43: No instance for (Data.Convertible.Base.Convertible t SqlValue) arising from a use of `toSql' at <interactive>:1:43-49 Possible fix: add an instance declaration for (Data.Convertible.Base.Convertible t SqlValue) In the expression: toSql 0 In the third argument of `run', namely `[toSql 0, toSql "zero"]' In the expression: run conn "INSERT INTO test VALUES (?, ?)" [toSql 0, toSql "zero"]
I think the problem is that 0 is polymorphic, so GHC does not know what type you want it to have, but its type determines which Convertible instance is chosen. It should work if you put a type signature on the 0, like toSql (0 :: Int) or toSql (0 :: Integer) or whatever. -Brent

...looks good, thank you - see following: [note: the '1' on the 2nd line
indicates a successful command execution] --
Prelude Database.HDBC Database.HDBC.Sqlite3> run conn "INSERT INTO test
VALUES (?, ?)" [toSql (0::Int), toSql ("zero"::[Char])]
1
----- Original Message -----
From: "Brent Yorgey"
On Thu, Feb 03, 2011 at 12:20:12PM -0500, Patrick Lynch wrote:
Good morning,
I'm using "Real World Haskell" and a Windows Vista PC and I have Sqlite3 installed...[note: I had to change test1.db to /users/user/test1.db in order to get this to work, otherwise, neither the database nor the table could be created -- also, note c:/users/user/test1.db gives a syntax error], ghci doesn't like c:/]:
I tried to use the following example from the book but it failed...see following:
Prelude> :m Database.HDBC Database.HDBC.Sqlite3 Prelude Database.HDBC Database.HDBC.Sqlite3> conn <- connectSqlite3 "/users/user/test1.db" ... Prelude Database.HDBC Database.HDBC.Sqlite3> run conn "INSERT INTO test VALUES (?, ?)" [toSql 0, toSql "zero"]
<interactive>:1:43: No instance for (Data.Convertible.Base.Convertible t SqlValue) arising from a use of `toSql' at <interactive>:1:43-49 Possible fix: add an instance declaration for (Data.Convertible.Base.Convertible t SqlValue) In the expression: toSql 0 In the third argument of `run', namely `[toSql 0, toSql "zero"]' In the expression: run conn "INSERT INTO test VALUES (?, ?)" [toSql 0, toSql "zero"]
I think the problem is that 0 is polymorphic, so GHC does not know what type you want it to have, but its type determines which Convertible instance is chosen. It should work if you put a type signature on the 0, like
toSql (0 :: Int)
or
toSql (0 :: Integer)
or whatever.
-Brent
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Thu, Feb 03, 2011 at 02:09:41PM -0500, Patrick Lynch wrote:
...looks good, thank you - see following: [note: the '1' on the 2nd line indicates a successful command execution] --
Prelude Database.HDBC Database.HDBC.Sqlite3> run conn "INSERT INTO test VALUES (?, ?)" [toSql (0::Int), toSql ("zero"::[Char])] 1
Great. Note that the ::[Char] annotation is not strictly necessary, since the type of "zero" is unambiguous*. But of course it can't hurt anything. -Brent * Well, unless you have enabled -XOverloadedStrings...

...BTW: do you know if there is a good tutorial on Haskell and Sqlite3....
...I'm pretty good at SQL, and if I can get thru Chapters 21, 22 and 23 in
"Real World Haskell"...I'd like to take a careful look at one...
...If there isn't a good tutorial available, I think I'll write one initally
for my own use and if it is any good I'll share it with the group...
...Overall, I found the installation of Sqlite3 on Windows Vista to be a
major challenge - I still don't think that I have the procedure to do it
again...but I need to install it on my Mac...not looking forward to
that...I've been fighting with the Apple support people already...bummer...
...As for installing gtk2hs, I'm getting nowhere with this...I'll tackle it
next week...
Thanks again for your help...I hope that I can repay your kindness
someday...
Ciao
----- Original Message -----
From: "Brent Yorgey"
On Thu, Feb 03, 2011 at 02:09:41PM -0500, Patrick Lynch wrote:
...looks good, thank you - see following: [note: the '1' on the 2nd line indicates a successful command execution] --
Prelude Database.HDBC Database.HDBC.Sqlite3> run conn "INSERT INTO test VALUES (?, ?)" [toSql (0::Int), toSql ("zero"::[Char])] 1
Great. Note that the ::[Char] annotation is not strictly necessary, since the type of "zero" is unambiguous*. But of course it can't hurt anything.
-Brent
* Well, unless you have enabled -XOverloadedStrings...

On Thu, Feb 03, 2011 at 04:37:19PM -0500, Patrick Lynch wrote:
...BTW: do you know if there is a good tutorial on Haskell and Sqlite3.... ...I'm pretty good at SQL, and if I can get thru Chapters 21, 22 and 23 in "Real World Haskell"...I'd like to take a careful look at one... ...If there isn't a good tutorial available, I think I'll write one initally for my own use and if it is any good I'll share it with the group...
I don't know, but if Google doesn't turn anything up, then there probably isn't. If you wrote one, I'm sure others would find it useful -- even a tutorial that isn't any good is a much better place to start than no tutorial at all! Just put it on a wiki and let others improve it.
Thanks again for your help...I hope that I can repay your kindness someday...
A beer should suffice! =) -Brent
participants (2)
-
Brent Yorgey
-
Patrick Lynch