<\/script>')

Ruby style question from a beginner

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 line, or am I being picky?

+1


a source to share


3 answers


Besides the loop in the hypoxic response, also consider using variable interpolation:

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

      



Note that your items were entry

best misguided or prepared for the world of pain, Bobby Tables style .

+4


a source


You can write a for loop for a string ... you would save a few lines of code.



+1


a source


What about:

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

      

+1


a source







All Articles