Class: Daru::IO::Exporters::SQL
- Defined in:
- lib/daru/io/exporters/sql.rb
Overview
SQL Exporter Class, that extends to_sql
method to Daru::DataFrame
instance variables
Instance Method Summary collapse
-
#initialize(dataframe, dbh, table) ⇒ SQL
constructor
Initializes a SQL Exporter instance.
-
#to ⇒ Object
Exports a SQL Exporter instance to an SQL table.
Methods inherited from Base
Constructor Details
#initialize(dataframe, dbh, table) ⇒ SQL
Initializes a SQL Exporter instance.
31 32 33 34 35 36 37 38 39 |
# File 'lib/daru/io/exporters/sql.rb', line 31 def initialize(dataframe, dbh, table) optional_gem 'dbd-sqlite3', requires: 'dbd/SQLite3' optional_gem 'dbi' optional_gem 'sqlite3' super(dataframe) @dbh = dbh @table = table end |
Instance Method Details
#to ⇒ Object
Exports a SQL Exporter instance to an SQL table.
45 46 47 48 49 50 51 |
# File 'lib/daru/io/exporters/sql.rb', line 45 def to query = "INSERT INTO #{@table} (#{@dataframe.vectors.to_a.join(',')}"\ ") VALUES (#{(['?']*@dataframe.vectors.size).join(',')})" sth = @dbh.prepare(query) @dataframe.each_row { |c| sth.execute(*c.to_a) } true end |