MYSQL insert

I'm trying to insert values from a variable into a test MYSQL. It works
from a fixed value but not if the value is in a variable.

require 'mysql'
$test = "ABC"

#db.query('insert into etf_list values ($test)')
    >> in `query': Column 'test' cannot be null (Mysql::Error)

db.query('insert into etf_list values ("ABC")')
    >> Works

Thanks
Jeff Bowen

···

--
Posted via http://www.ruby-forum.com/.

use double quotes, and enclose with \"#{}\". single quotes don't
expand variables.
i.e. db.query(|insert into etf_list values (\"#{$test}\")")

j.

···

On Mon, Jul 13, 2009 at 17:12, Jeff Bowen<ja_bowen@yahoo.com> wrote:

I'm trying to insert values from a variable into a test MYSQL. It works
from a fixed value but not if the value is in a variable.

require 'mysql'
$test = "ABC"

#db.query('insert into etf_list values ($test)')
>> in `query': Column 'test' cannot be null (Mysql::Error)

Also, this is ruby, not perl. Unless you have a "really good reason"(tm) for using $test which is a global variable, you probably just need a plain old local varialble here:

test = "ABC" # or test='ABC' since either style of quotes is fine for most strings with no interpolation

db.query("insert into etf_list values (\"#{test}\")")

There's actually another form of string literal that I often find useful when quotes are concerned: %{}
(and you can leave out the parentheses in many instances and, IMHO, this is one)[1]

db.query %{insert into etf_list values ("#{test}")}

-Rob

[1] You might also take a few minutes to read James Edward Gray II's
     post about when to use parentheses and when to leave them out.
     Gray Soft / Not Found

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Jul 13, 2009, at 11:24 AM, Jano Svitok wrote:

On Mon, Jul 13, 2009 at 17:12, Jeff Bowen<ja_bowen@yahoo.com> wrote:

I'm trying to insert values from a variable into a test MYSQL. It works
from a fixed value but not if the value is in a variable.

require 'mysql'
$test = "ABC"

#db.query('insert into etf_list values ($test)')
   >> in `query': Column 'test' cannot be null (Mysql::Error)

use double quotes, and enclose with \"#{}\". single quotes don't
expand variables.
i.e. db.query(|insert into etf_list values (\"#{$test}\")")

j.