Skip to content

Commit b645293

Browse files
committed
initial commit
1 parent d65ee09 commit b645293

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
WikiLeaks2MySQL
22
===============
33

4-
Here is a ruby script for importing the wikileak cables.csv file to a MySQL database.
4+
Here is a ruby script for importing the wikileak cables.csv file to a MySQL database.
5+
6+
You can grab the cable file from http://archive.org/details/wikileaks-cables-csv.
7+
8+
Feel free to modify and amend! A database with the name 'wikileak' is required, than again feel free to change. Enjoy!

wikileaks2mysql.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'rubygems'
2+
require 'mysql'
3+
database_name = "wikileak"
4+
table_name = "cable"
5+
csvfile = '/tmp/pathtocablefile/cables.csv'
6+
m = Mysql.new("localhost", "username", "password")
7+
m.select_db(database_name)
8+
m.query("DROP TABLE IF EXISTS "+table_name)
9+
ptable_create = "CREATE TABLE "+table_name+"(
10+
id INT auto_increment primary key,
11+
date CHAR(20),
12+
code CHAR(50),
13+
embassy CHAR(100),
14+
class CHAR(50),
15+
state CHAR(50),
16+
sendto CHAR(200),
17+
cabletext TEXT);"
18+
m.query(ptable_create)
19+
def save_to_db(cable_entry,table_name,m)
20+
entry_field = 0
21+
db_entry = []
22+
db_entry[7] = ''
23+
#Remove unnecessary Commas
24+
cable_entry = cable_entry.gsub('","',"$$")
25+
cable_entry = cable_entry.gsub(",","")
26+
cable_entry = cable_entry.gsub("$$",'","')
27+
save_entry = cable_entry.split(",")
28+
save_entry.each do |da_entry|
29+
if entry_field == 7
30+
db_entry[7] = db_entry[7]+da_entry
31+
else
32+
db_entry[entry_field] = da_entry
33+
entry_field = entry_field + 1
34+
end
35+
valo = 0
36+
end
37+
pquery = "INSERT INTO `"+table_name+"` VALUES("+db_entry[0]+","+db_entry[1]+","+db_entry[2]+","+db_entry[3]+","+db_entry[4]+","+db_entry[5]+","+db_entry[6]+","+db_entry[7]+");"
38+
m.query(pquery)
39+
end
40+
cable_entry = ''
41+
seeknum = 1
42+
seeker = '"'+seeknum.to_s()+'"'
43+
File.open(csvfile).each do |record|
44+
if record.match(seeker)
45+
if seeknum > 1
46+
save_to_db(cable_entry,table_name,m)
47+
puts "Saves: "+seeknum.to_s()
48+
end
49+
cable_entry = record
50+
seeknum += 1
51+
c_record_num = 1
52+
seeker = '"'+seeknum.to_s()+'"'
53+
else
54+
cable_entry = cable_entry+record
55+
end
56+
end
57+
m.close

0 commit comments

Comments
 (0)