From: Rob Biedenharn Date: 2009-07-14T00:48:32+09:00 Subject: Re: MYSQL insert On Jul 13, 2009, at 11:24 AM, Jano Svitok wrote: > On Mon, Jul 13, 2009 at 17:12, Jeff Bowen 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. 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. http://blog.grayproductions.net/articles/ do_i_need_these_parentheses Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com