//
you're reading...
programming, ruby, Tips'n'Tricks

Mixing quoted and non quoted values with Ruby DBI prepared statements

Sorry for the somewhat long title, but I messed a bit with DBI in Ruby today, and I had some problems with prepared statements, as DBI quotes everything. I wanted to do something like the following:

sth = dbh.prepare(“UPDATE table SET ? = ? WHERE id = ‘1’”)

With a normal approach, both values would be quoted if I ran sth.execute(“field”, “value”), but as the sth.execute runs a quote function, which is just an ugly while loop, and the else part of it, is just value.to_s, you can do it quite smart:

sth.execute(:field, “value”)

Isn’t that nice? I think so.

Discussion

One thought on “Mixing quoted and non quoted values with Ruby DBI prepared statements

  1. Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.

    Posted by sandrar | September 10, 2009, 1:15 pm

Leave a comment