Class: Database

Inherits:
Object
  • Object
show all
Defined in:
lib/csvr/sqlitedb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, table, headers, rows) ⇒ Database

Returns a new instance of Database.



7
8
9
10
11
12
# File 'lib/csvr/sqlitedb.rb', line 7

def initialize(db, table, headers, rows)
  @db = db
  @table = table
  @headers = headers
  @rows = rows
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



5
6
7
# File 'lib/csvr/sqlitedb.rb', line 5

def db
  @db
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/csvr/sqlitedb.rb', line 5

def headers
  @headers
end

#rowsObject (readonly)

Returns the value of attribute rows.



5
6
7
# File 'lib/csvr/sqlitedb.rb', line 5

def rows
  @rows
end

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/csvr/sqlitedb.rb', line 5

def table
  @table
end

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
# File 'lib/csvr/sqlitedb.rb', line 14

def create

  puts "Inserting #{@rows.size} rows into database"

  db = SQLite3::Database.open("#{@db}.db")
  db.execute "CREATE TABLE IF NOT EXISTS #{@table}(#{@headers})"
  @rows.each { |row| db.execute "INSERT INTO #{@table} #{row}" }
  db.close
end