tags:

views:

89

answers:

3
USER = "user"
PASS = "pass"

QUERY = "SELECT c1, c2, c3, c4, c5, c6, c7 FROM table"

SQLITE_SCHEMA = 'c1, c2, c3, c4, c5, c6, c7'

sqlite_db = SQLite3::Database.new('sqlite.db')
odbc_db = DBI.connect('DBI:ODBC:database', USER, PASS)
odbc_db.select_all(QUERY) do |entry|
  sqlite_db.execute "insert into smnt (" + SQLITE_SCHEMA + ") values ('" + entry[0].to_s + "','" + 
                                                                  entry[1].to_s + "','" + 
                                                                           entry[2].to_s + "','" +
                                                                           entry[3].to_s + "','" +
                                                                           entry[4].to_s + "','" + 
                                                                           entry[5].to_s + "','" +
                                                                           entry[6].to_s + "')" 
  end

There must be a cleaner way to write the db.execute string, or am I being picky?

+1  A: 

You could write a for loop for the string... you'd save a few lines of code.

hypoxide
+3  A: 

Apart from the loop in hypoxide's answer, also consider using variable interpolation:

sqlite_db.execute "insert into smnt (#{SQLITE_SCHEMA}) 
  values (#{entry.map {|e| "'#{e.to_s}'"}.join(',')})"

Note that your entry elements had better all be sanitised, or be prepared for a world of pain, Bobby Tables style.

Chris Jester-Young
damn ... beat me to it :)
Toby Hede
*lol* I went whole hog and avoided any use of + at all, but that's just my preference. :-P
Chris Jester-Young
I really like this. I need to train myself to think this way.
rubynewbie
Haha. Superb answer.
hypoxide
+1  A: 

How about:

" VALUES (#{entry.collect{|e|"'"+e.to_s+"'"}.join(",")})"
Toby Hede