sqlite3 insert statement:
db.execute "INSERT INTO events(#{keys.join(',')}) VALUES (#{(['?'] *
keys.size).join(',')})", *values
I'm very confused about this part:
(#{(['?'] * keys.size).join(',')})", *values
how can I write a mysql insert statement that functions the same?
Thanks a lot!
on 2013-03-01 01:07
on 2013-03-01 01:30
On Thu, Feb 28, 2013 at 6:08 PM, Yu W. <lists@ruby-forum.com> wrote: > sqlite3 insert statement: > db.execute "INSERT INTO events(#{keys.join(',')}) VALUES (#{(['?'] * > keys.size).join(',')})", *values > > I'm very confused about this part: > (#{(['?'] * keys.size).join(',')})", *values It's basically creating a string with a certain number of question marks, and then supplying values at the end to be substituted for those question marks. E.g. if keys is ['foo' 'bar' 'baz'] and values is [123, 456, 789], it will generate the command (with placeholders) 'INSERT INTO events(foo, bar, baz) VALUES (?, ?, ?)'; db.execute then plugs in 123, 456, and 789 into the question mark slots. > > how can I write a mysql insert statement that functions the same? Have you tried it in MySQL? It looks like it should work just the same in pretty much any SQL engine.
on 2013-03-01 02:18
Thanks a lot for reply! I've tried mysql gem. But it doesn't allow
expression like this
"(#{(['?'] * keys.size).join(',')})", *values"
it says mysql query method only allows one parameter.
on 2013-03-01 02:41
On Thu, Feb 28, 2013 at 7:18 PM, Yu W. <lists@ruby-forum.com> wrote: > Thanks a lot for reply! I've tried mysql gem. But it doesn't allow > expression like this > "(#{(['?'] * keys.size).join(',')})", *values" > > it says mysql query method only allows one parameter. Oh, sorry. It's been a while since I did this, but I believe you have to call Mysql#prepare on the string containing the '?' placeholders, which returns a Mysql::Stmt object. Call #execute on that object with the *values. That's how it works with the Mysql2 gem, anyway; see http://zetcode.com/db/mysqlrubytutorial/ .
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.