update-values

clojure.contrib.sql

  • (update-values table where-params record)
Updates values on selected rows in a table. where-params is a vector
containing a string providing the (optionally parameterized) selection
criteria followed by values for any parameters. record is a map from
strings or keywords (identifying columns) to updated values.

1 Example top

  • (update-values :users                 ;; table name is 'users'
                   ["fname=?" "guest"]    ;; this is same that 
                   {:fname "ezcocoa"}))   ;; update users set fname='ezcocoa' where fname='guest'
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/sql.clj:168 top

(defn update-values
  "Updates values on selected rows in a table. where-params is a vector
  containing a string providing the (optionally parameterized) selection
  criteria followed by values for any parameters. record is a map from
  strings or keywords (identifying columns) to updated values."
  [table where-params record]
  (let [[where & params] where-params
        column-strs (map as-str (keys record))
        columns (apply str (concat (interpose "=?, " column-strs) "=?"))]
    (do-prepared
     (format "UPDATE %s SET %s WHERE %s"
             (as-str table) columns where)
     (concat (vals record) params))))
Vars in clojure.contrib.sql/update-values: do-prepared concat defn format let vals
Used in 1 other vars update-values

Comments top

2 comment(s) for update-values.

How do I set the where clause to 1=1? In other words, if I wanted to update all rows. You can't exactly put "1=1" as a string, right?

Also, what if I wanted to update where a column is null. Simply saying ["columnName=?" nil] doesn't suffice because the where statement columnName = NULL doesn't work. How do I translate "where columnName is NULL" to clojure code?

Log in to add a comment.