# MYSQL insert

**URL:** <https://www.ruby-forum.com/t/mysql-insert/172033>\
**Category:** Ruby\
**Created:** [July 13, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/mysql-insert/172033 "2009-07-13T17:12:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbell38](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jbell38](https://www.ruby-forum.com/u/jbell38)\
**Post date:** [July 13, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/mysql-insert/172033/1 "2009-07-13T17:12:04Z")

</div>

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 B.

---

<div class="post-metadata">

**Author:** ![jbell38](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jbell38](https://www.ruby-forum.com/u/jbell38)\
**Post date:** [July 13, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/mysql-insert/172033/2 "2009-07-13T17:24:58Z")

</div>

On Mon, Jul 13, 2009 at 17:12, Jeff B.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![jbell38](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@jbell38](https://www.ruby-forum.com/u/jbell38)\
**Post date:** [July 13, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/mysql-insert/172033/3 "2009-07-13T17:50:41Z")

</div>

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

> 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"™ 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 G. II’s  
post about when to use parentheses and when to leave them out.  
[http://blog.grayproductions.net/articles/](http://blog.grayproductions.net/articles/)  
do\_i\_need\_these\_parentheses

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
