Use Hash with Redundant Items in sqlite3 Bind

ruby, sqlite3-ruby

Solution

You should write as -

Query = "INSERT INTO t (x,y,z) VALUES (?,?,?)"
hash = {"aval" => "1", "bval" => "2", "cval" => "3"}
db.execute(Query, hash.values_at("aval", "bval", "cval" ))

Read this `execute( sql, *bind_vars ) {|row| ...}` documentation.

Problem

I'm trying to run an INSERT query such as the following: ``` Query = "INSERT INTO t (x,y,z) VALUES (:aval,:bval,:cval)" ``` Using the following format, I can use a hash to insert the actual values: ``` db.execute(Query,{"aval" => "1", "bval" => "2", "cval" => "3"}) ``` My problem is that the values are already in a hash that has some redundant values, e.g.: ``` {"aval" => "1", "bval" => "2", "cval" => "3", "dval" => "4"} ``` Since dval is not one of the required parameters, I get the error - ``` SQLite3::Exception: no such bind parameter ``` Of course, I may be wrong and the error may be due to a different reason. It would be great if there were a way to overcome this using SQLite3. Alternatively, a method for creating a "trimmed" copy of the has with only the required parameters would also be OK.

Original source