From: Barry Sperling Date: 2005-02-10T02:53:45+09:00 Subject: Re: DBI INSERT with variables Thanks for the quick response, Gavin, but I don't see how you are making the field names variables. The names "title", "date_updated" and "author" seem to be the same with only the value of the title changing. I would like to loop through a list of field names assigning something to one field, and they something else to another field, which would require a variable of some kind in the field section, not just the value section. Any examples? Barry gavin@refinery.com wrote: > I'm on WinXP, using Ruby 1.8.2 with DBI to connect to a MSSQL2k DB via > ODBC DSN. > > The following works for me: > > LIVE_DB_DSN_LOGIN = [ 'dbi:ODBC:LiveServer', 'myusername', > 'mypassword' ] > > begin > require 'dbi' > > dbh = DBI.connect( *LIVE_DB_DSN_LOGIN ) > > add_article = dbh.prepare( <<-ENDSQL > INSERT INTO tblArticles > ( title, date_updated, author ) > VALUES ( ?, #{Time.at(0).to_sql}, '#{AUTHOR}' ) > ENDSQL > ) > > #...later > > add_article.execute( title ) > > > rescue DBI::DatabaseError => e > puts "An error occurred" > puts "Error code: #{e.err}" > puts "Error message: #{e.errstr}" > > > ensure > #close out statements > add_article.finish > > #unplug from the database > dbh.disconnect > > end > >