Forums | Admin

Discussion Forums: help

Start New Thread Start New Thread
Message: 66334
BY: Gavin Kistner (phrogz)
DATE: 2009-03-20 04:09
SUBJECT: RE: help with array to sqlite3

 

1) Install SQLite3
2) sudo gem install sqlite3-ruby
3) sudo gem install sequel

Slim2:Desktop phrogz$ cat words.txt
["ἀβαρής", "not burdensome", "adjective", "1"]
["ascii", "easy", "noun", "0"]
["utf", "ftw", "noun", "0"]
["awesome","ruby","adjective","3"]

Slim2:Desktop phrogz$ cat convertor.rb
require 'rubygems'
require 'sequel'

SOURCE_FILE = 'words.txt'
DATABASE_FILE = 'words.db'

# This will create the file if it doesn't exist, or use it if it does
DB = Sequel.sqlite( DATABASE_FILE )

DB.drop_table :words
DB.create_table :words do
primary_key :id
column :greek, :text
column :english, :float
column :part_of_speech, :integer
column :occurrences, :integer
end

DB.drop_table :parts_of_speech
DB.create_table :parts_of_speech do
primary_key :id
column :name, :text
end

PART_ID_BY_NAME = {}

words = DB[:words]
parts = DB[:parts_of_speech]

File.foreach( SOURCE_FILE ) do |line|
pieces = line.scan( /"([^"]+)"/ ).flatten
gr, en, part, hits = *pieces

# This test to skip blank lines in the file
if gr then
part_id = PART_ID_BY_NAME[ part ]
unless part_id
part_id = ( parts << { :name => part } )
PART_ID_BY_NAME[ part ] = part_id
end
words << {
:greek => gr,
:english => en,
:part_of_speech => part_id,
:occurrences => hits.to_i
}
end
end

Slim2:Desktop phrogz$ ruby convertor.rb
Slim2:Desktop phrogz$ sqlite3 words.db
SQLite version 3.4.0
Enter ".help" for instructions
sqlite> .schema
CREATE TABLE `parts_of_speech` (`id` integer PRIMARY KEY AUTOINCREMENT, `name` text);
CREATE TABLE `words` (`id` integer PRIMARY KEY AUTOINCREMENT, `greek` text, `english` float, `part_of_speech` integer, `occurrences` integer);
sqlite> .headers on
sqlite> select * from words;
id|greek|english|part_of_speech|occurrences
1|ἀβαρής|not burdensome|1|1
2|ascii|easy|2|0
3|utf|ftw|2|0
4|awesome|ruby|1|3
sqlite> select * from parts_of_speech;
id|name
1|adjective
2|noun
sqlite> .quit


Thread View

Thread Author Date
help with array to sqlite3k h2009-03-19 18:13
      RE: help with array to sqlite3Gavin Kistner2009-03-19 23:22
            RE: help with array to sqlite3k h2009-03-20 00:09
                  RE: help with array to sqlite3k h2009-03-20 00:13
                  RE: help with array to sqlite3Gavin Kistner2009-03-20 04:09
                        RE: help with array to sqlite3Gavin Kistner2009-03-20 04:43
                              RE: help with array to sqlite3Gavin Kistner2009-03-20 05:03

Post a followup to this message