Class: CsvToSqlite::SQL::Insert
- Inherits:
-
Object
- Object
- CsvToSqlite::SQL::Insert
- Defined in:
- lib/sql/insert.rb
Instance Method Summary collapse
-
#initialize(connection:, csv_table:, name:) ⇒ Insert
constructor
A new instance of Insert.
- #run ⇒ Object
Constructor Details
#initialize(connection:, csv_table:, name:) ⇒ Insert
Returns a new instance of Insert.
7 8 9 10 11 |
# File 'lib/sql/insert.rb', line 7 def initialize connection:, csv_table:, name: @connection = connection @csv_table = csv_table @name = name end |
Instance Method Details
#run ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sql/insert.rb', line 13 def run columns = @csv_table.headers.sort = TTY::ProgressBar.new("Inserting data [:bar] :percent", total: @csv_table.by_row.count) @csv_table.by_row.each do |row| values = columns.map { |column| row[column] } question_marks = ("?," * columns.size).chop! @connection.execute("INSERT INTO #{@name} (#{columns.join(',')}) VALUES (#{question_marks})", values) .advance(1) end .finish end |