Skip to content

Instantly share code, notes, and snippets.

@venj
Created July 4, 2011 03:07
Show Gist options
  • Save venj/1062854 to your computer and use it in GitHub Desktop.
Save venj/1062854 to your computer and use it in GitHub Desktop.
gen database file for hsk app
#!/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