Created
July 4, 2011 03:07
-
-
Save venj/1062854 to your computer and use it in GitHub Desktop.
gen database file for hsk app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "sqlite3" | |
dbfile = "data.sqlite3" | |
File.unlink dbfile if File.exists? dbfile | |
db = SQLite3::Database.new dbfile | |
db.execute("create table `entries` (id integer primary key autoincrement, question string, choice1 string, choice2 string, answer string);") | |
infile = open "dd.txt", 'r' | |
infile.readlines.each_slice(3) do |s| | |
s.collect(&:strip!) | |
choices = s[1].split(/\s+/).collect(&:strip) | |
db.execute("insert into `entries` (question, choice1, choice2, answer) values (?,?,?,?);", s[0], choices[0], choices[1], s[2]) | |
end | |
db.close | |
infile.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment