-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathimport.py
More file actions
130 lines (96 loc) · 2.97 KB
/
Copy pathimport.py
File metadata and controls
130 lines (96 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import sys, time
import MySQLdb as mysql
class CSVImport:
def printhelp(self):
print """
******************************************
MYSQL Easy CSV Import Command Line Client
******************************************
host: your mysql host
user: your mysql username
password: your database to import csv file to
columns: the line number with column headings in the csv
delimiter: the delimiter used to separate values
table: the table to create and populate with values
usage:
python import.py host=123.456.789.456 un=someguy pw=fdsarewq db=somedb
>file: file.csv columns=0 delimiter=, table=myTable
"""
def __init__(self):
if sys.argv[1] == '--help':
self.printhelp()
elif sys.argv[1] == '--gui':
self.gui()
else:
try:
args = dict([arg.split('=') for arg in sys.argv[1:]])
self.db = mysql.connect(args['host'], args['un'], args['pw'], args['db'])
self.cursor = self.db.cursor(mysql.cursors.DictCursor)
print("connected to "+args['host']+"\n"+"database: "+args['db']+" selected\n")
fname = raw_input("file: ")
if fname.find(".csv") != -1:
args = fname.split(" ")
columns = 0
delimiter = ','
table = args[0].split(".")[0]
for x in args:
if x.find("columns") != -1:
columns = x.split("=")[1]
elif x.find("delimiter") != -1:
delimiter = x.split("=")[1]
elif x.find("table") != -1:
table = x.split("=")[1]
self.readFile(args[0], delimiter, columns, table)
else:
print("file does not have .csv extension\n is this really a csv? (kindof important) try again\n")
except Exception, e:
for x in e.args:
print x
sys.exit(1)
def readFile(self, filename, delimiter, columns, table=''):
csv = open(filename)
for x in range(int(columns)+1):
c = csv.readline()
columns = c.split(delimiter)
cols = ''
for x in columns:
cols+=x+" varchar(255), "
cols = cols[:-2]
sql = "create table if not exists "+table.replace("-", "_")+" (id int NOT NULL AUTO_INCREMENT, "+cols+", PRIMARY KEY (id))"
try:
self.cursor.execute(sql)
except Exception, e:
print e
print "Here we go!!"
cols = ''
for x in columns:
cols+=x+", "
cols = cols[:-2]
start = time.time()
while True:
row = csv.readline()
if not row:
break
else:
row = row.split(delimiter)
values = ''
for x in row:
values += '"'+self.clean(x)+'", '
values = values[:-2]
sql = "insert into "+table.replace("-", "_").lower()+" ("+cols+") values ("+values+")"
try:
self.cursor.execute(sql)
except Exception, e:
self.db.commit()
print e
self.db.commit()
end = time.time()
print "\nAll DONE EVERYTHING WORKED!!\ndam that was fast...\nstarted at: "
print start
print "\nended at: "
print end
print "\noperation took: "
print end-start
def clean(self, string):
return string.replace('"', "").replace("\n", "")
CSVImport()