-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinsert_data.py
47 lines (34 loc) · 1.33 KB
/
insert_data.py
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
#!/usr/bin/env python
import MySQLdb
import csv
import argparse
# Simple python script for ingesting data from csv-file
def main():
# read command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("database", type=str, help="Name of database")
parser.add_argument("table", type=str, help="Name of database table")
parser.add_argument("csvfile", type=str, help="Name of the csv file with the data")
args = parser.parse_args()
database = args.database
table = args.table
csvfile = args.csvfile
# connect with database
db = MySQLdb.connect(host="spider00", user="root", db=args.database)
cur = db.cursor()
# read csv-file with data
fh = open(csvfile, 'r')
csvreader = csv.reader(fh, delimiter=',', lineterminator='\n')
# find number of columns (from first line) for constructing ingest-string
ncolumns = len(next(csvreader))
fh.seek(0) # go back
valuestring = "%s, " * (ncolumns-1) + "%s"
sql = "INSERT INTO `%s`.`%s` VALUES (%s)" % (database, table, valuestring)
# run the sql command, row by row (it's not fast, but easy ...)
print "Ingesting data ..."
cur.executemany(sql, csvreader)
cur.close()
fh.close()
print "Ingest done for table %s.%s from file %s." % (database, table, csvfile)
if __name__ == '__main__':
main()