Ruby + mysql

Sono nelal melma con MySql
Voglio inserire un valore in un campo di una maschera video e voglio
cercare se questo valore esiste in una data colonna di una tabella
qualsiasi: quindi

“SELECT * FROM tabella WHERE variabile = colonna_tabella”

come faccio a dire a MySql che mi deve cercare il contenuto di
“variabile” e non la stringa “variabile” nella colonna specificata?

Lo stesso problema si pone per il comando insert: tutti gli esempi che
ho trovato riportano:

INSERT INTO tabella VALUES (‘valore1’,‘valore2’, …)

se valore1 e’ una variabile con un valore qualsiasi come devo fare per:
dire a MySql che “valore1” e’ una variabile;
di inserire nel campo del database il contenuto di "valore1 e non la
stringa “valore1” con Ruby?

Grazie e saluti

On Sep 21, 2007, at 5:17 AM, Guerra A. wrote:

Sono nelal melma con MySql
Voglio inserire un valore in un campo di una maschera video e voglio
cercare se questo valore esiste in una data colonna di una tabella
qualsiasi: quindi

“SELECT * FROM tabella WHERE variabile = colonna_tabella”

Have you tried using ActiveRecord? You can get it by typing
gem install activerecord

I use it a lot for my database, and it makes my code pretty :slight_smile: I
recommend it to you if you’re doing anything with MySQL.

-------------------------------------------------------|
~ Ari
crap my sig won’t fit

Lo stesso problema si pone per il comando insert: tutti gli esempi che
ho trovato riportano:

INSERT INTO tabella VALUES (‘valore1’,‘valore2’, …)

se valore1 e’ una variabile con un valore qualsiasi come devo fare per:
dire a MySql che “valore1” e’ una variabile;
di inserire nel campo del database il contenuto di "valore1 e non la
stringa “valore1” con Ruby?

Rough translation to English: How do I insert a value into the table
without literalizing it, so the value in the table would be the value
of the variable named valore1, instead of ‘valore1’.

The simple answer to your question is this:

sql = “INSERT INTO tabella VALUES (‘#{valore1}’, …)”
dbc.query(sql)

You can do the same with select statements:

sql = “SELECT * FROM tabella WHERE ‘#{variable}’ = colonna_tabella”
dbc.query(sql)

However you’ll soon run into SQL injection problems, so this is not
really the best way to go about it. May I suggest you use an ORM
library like Sequel, and then you can do stuff like:

DB[:tabella].insert(valore1, valore2)

DB[:tabella].filter {:colonna_tabella == variable}

Sequel will take care of proper string literalization and protect you
from SQL injection.

More info here:

Google Code Archive - Long-term storage for Google Code Project Hosting.

best
Sharon

Thank you a lot, Sharon!!!
Do you speak italian?
I would have to post this question in the italian forum but a wrong
command put it in this international forum.

On Sep 21, 2007, at 10:25 AM, Sharon R. wrote:

Rough translation to English: How do I insert a value into the table
without literalizing it, so the value in the table would be the value
of the variable named valore1, instead of ‘valore1’.

Ah. yes… well… that certainly makes MY response invalid.

---------------------------------------------------------------|
~Ari
“I don’t suffer from insanity. I enjoy every minute of it” --1337est
man alive

Thank you a lot, Sharon!!!
Do you speak italian?

I can read more or less. Glad to have helped.

sharon