From: David Alan Black Date: 2002-05-19T21:34:16+09:00 Subject: Re: passing CGI parameters to ruby MySQL interface Hello -- On Sun, 19 May 2002, sentinel wrote: > Hi, > > I'm spending a lot of time trying to pass cgi.parameters to a MySQL > database > via the ruby MySQL module. > > For instance, using > > $my.query("insert into agent(_case, name1, name2) values > (cgi.params['_case'], cgi.params['name1'], cgi.params['name2'])") > > it will write to the database, but the values will be literally what > is shown - i.e. cgi.params['name2'], etc. > > I'm probably missing something obvious, but how can I insert the hash > values into the INSERT values ? One way is to use Ruby's capacity to interpolate arbitrary expressions into strings, using the #{} notation: cp = cgi.params # just to save space :-) $my.query("insert into agent(_case, name1, name2) values (#{cp['_case']}, #{cp['name1']}, #{cp['name2']})") or if you want to be more encapsulated as to variables (untested code follows): vals = %w{ _case name1 name2 } ag_args = vals.join(",") va_args = vals.map {|v| cgi.params[v]}.join(",") $my.query("insert into agent(#{ag_args}) values(#{va_args})") and perhaps wrap it up in a method if you use it a lot. David P.S. Do you really need that global variable...? :-) -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav