Forums | Admin

Discussion Forums: help

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

 

Alright, one final email. This one is tested and fixes a few bugs.

I really wish RubyForge had information on what markup, if any, was allowed in the forums. Or let you edit your posts after the fact. Wrapping this code in a few to see if any of it works:

pre.. @[pre]{{{
require 'rubygems'
require 'sqlite3'

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

db = SQLite3::Database.new( DATABASE_FILE )
make_tables = <<ENDSQL
DROP TABLE parts_of_speech;
CREATE TABLE parts_of_speech (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT
);
DROP TABLE words;
CREATE TABLE words (
id INTEGER PRIMARY KEY AUTOINCREMENT,
greek TEXT,
english TEXT,
part_of_speech_id INTEGER,
occurrences INTEGER
);
ENDSQL

PART_ID_BY_NAME = {}

db.execute_batch( make_tables );
db.transaction do
File.foreach( SOURCE_FILE ) do |line|
pieces = line.scan( /"([^"]+)"/ ).flatten
gr, en, part, hits = *pieces
if gr && en && part && hits then
part_id = PART_ID_BY_NAME[ part ]
unless part_id
db.execute( "insert into parts_of_speech (name) values (?)", part )
part_id = db.last_insert_row_id
PART_ID_BY_NAME[ part ] = part_id
end
db.execute(
"insert into words
( greek, english, part_of_speech_id, occurrences )
values (?,?,?,?)",
gr, en, part_id, hits.to_i
)
end
end
end
}}}[/pre]@


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