views:

81

answers:

2

How to insert a blob in database using the clojure.contrib.sql?

I've tried the following reading from a file but I'm getting this exception:

SQLException: Message: Invalid column type SQLState: 99999 Error Code: 17004 java.lang.Exception: transaction rolled back: Invalid column type (repl-1:125)

(clojure.contrib.sql/with-connection
   db
   (clojure.contrib.sql/transaction
    (clojure.contrib.sql/insert-values :test_blob [:blob_id :a_blob] [3   (FileInputStream. "c:/somefile.xls")]) ))

Thanks.

A: 

I believe it's just the same way you'd insert any other value: use one of insert-records, insert-rows or insert-values. E.g.:

(insert-values :mytable [:id :blobcolumn] [42 blob])

More examples: http://github.com/richhickey/clojure-contrib/blob/master/src/test/clojure/clojure/contrib/test_sql.clj

harto
It's not working if I want to save a file as a BLOB.
aQ123
Oh. I don't imagine `FileInputStream` is serializable - I wonder if that's the problem.
harto
+1  A: 

I was able to solve this by converting the FileInputStream into a ByteArray.

(clojure.contrib.sql/with-connection 
   db 
   (clojure.contrib.sql/transaction 
    (clojure.contrib.sql/insert-values :test_blob [:blob_id :a_blob] [3   (to-byte-array(FileInputStream. "c:/somefile.xls"))]) )) 
aQ123