MYSQL insert

J

Jeff Bowen

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)')
db.query('insert into etf_list values ("ABC")')
Thanks
Jeff Bowen
 
J

Jano Svitok

I'm trying to insert values from a variable into a test MYSQL. =A0It work= s
from a fixed value but not if the value is in a variable.

require 'mysql'
$test =3D "ABC"

#db.query('insert into etf_list values ($test)')
=A0 =A0>> 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.
 
R

Rob Biedenharn

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top