Ruby MySQL INSERT variable

k = “50”

my = Mysql::new(“localhost”, “root”, “pass”, “table”)
my.query(“INSERT INTO table VALUES(‘2008-05’, #{k}, 324)”)

./Class.rb:212:in `query’: Unknown column ‘50’ in ‘field list’
(Mysql::Error)

How do I insert a variable into a row?

Thanks

Well, i solved this by bringing ( ’ ’ ) around the #{k}. Thus your code
is
likely to become like this,
my = Mysql::new(“localhost”, “root”, “pass”, “table”)
my.query(“INSERT INTO table VALUES(‘2008-05’, ‘#{k}’, 324)”)

Hope it solves it.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs