user3639782
user3639782

Reputation: 517

jdbc/insert! on sqlite3 does not manage more than two rows

I am trying to batch-write to a sqlite3 db using a pooled connection as described in clojure-cookbook.

It works up to two rows. When I insert three rows I got a java.lang.ClassCastException: clojure.lang.MapEntry cannot be cast to clojure.lang.Named exception.

Here's my code:

(def db-spec {:classname   "org.sqlite.JDBC"                                                                                                                                                                                                                                    
              :subprotocol  "sqlite"                                                                                                                                                                                                                                            
              :subname "sqlite.db"                                                                                                                                                                                                                                              
              :init-pool-size 1                                                                                                                                                                                                                                                 
              :max-pool-size 1                                                                                                                                                                                                                                                  
              :partitions 1})                                                                                                                                                                                                                                                   

(jdbc/db-do-commands                                                                                                                                                                                                                                                            
    *pooled-db*                                                                                                                                                                                                                                                                 
    (jdbc/create-table-ddl                                                                                                                                                                                                                                                      
      :play                                                                                                                                                                                                                                                                     
      [[:primary_id :integer "PRIMARY KEY AUTOINCREMENT"]                                                                                                                                                                                                                       
       [:data :text]]))                                                                                                                                                                                                                                                         

(jdbc/insert! *pooled-db* :play {:data "hello"}{:data "hello"})                                                                                                                                                                                                                 
(jdbc/insert! *pooled-db* :play {:data "hello"}{:data "hello"}{:data "hello"})   

What am I missing here?

Thanks

Upvotes: 0

Views: 268

Answers (1)

Alan Thompson
Alan Thompson

Reputation: 29976

See the docs for this example: https://github.com/clojure/java.jdbc

(j/insert-multi! mysql-db :fruit
  [{:name "Apple" :appearance "rosy" :cost 24}
   {:name "Orange" :appearance "round" :cost 49}])

The API docs say this:

insert-multi!
function
Usage: (insert-multi! db table rows)
       (insert-multi! db table cols-or-rows values-or-opts)
       (insert-multi! db table cols values opts)
Given a database connection, a table name and either a sequence of maps (for
rows) or a sequence of column names, followed by a sequence of vectors (for
the values in each row), and possibly a map of options, insert that data into
the database.
When inserting rows as a sequence of maps, the result is a sequence of the
generated keys, if available (note: PostgreSQL returns the whole rows).
When inserting rows as a sequence of lists of column values, the result is
a sequence of the counts of rows affected (a sequence of 1's), if available.
Yes, that is singularly unhelpful. Thank you getUpdateCount and executeBatch!
The :transaction? option specifies whether to run in a transaction or not.
The default is true (use a transaction). The :entities option specifies how
to convert the table name and column names to SQL entities.

Upvotes: 1

Related Questions