This repository has been archived by the owner on Mar 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdataDump.py
82 lines (61 loc) · 2.04 KB
/
dataDump.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
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
#CSV extract from database table
import sys
import csv
import string
from datetime import datetime
import utils # General utils including config params and database connection
if len(sys.argv) < 3:
print(len(sys.argv))
print("\nUsage: {0} <tablename> <database_name> [ <filename.csv> ] \n".format(sys.argv[0]))
exit(1)
if len(sys.argv) > 3:
dumpfile = sys.argv[3]
else:
dumpfile = ""
tablename = sys.argv[1]
DBNAME = sys.argv[2]
conf = utils.get_config()
DBHOST = conf["MySQL"]["server"]
DBUSER = conf["MySQL"]["dbuser"]
DBCHARSET = conf["MySQL"]["dbcharset"]
def containsAny(s, set):
"""Check whether 'str' contains ANY of the chars in 'set'"""
return 1 in [c in str(s) for c in set]
# MAIN #
print("Connecting to database...", end=" ")
connection = utils.db_connection(DBHOST, DBUSER, DBNAME, DBCHARSET)
cursor = connection.cursor()
print("connected.")
## Dump Data ##
statement = 'SELECT * FROM ' + tablename
print("EXECUTING", statement)
cursor.execute(statement)
fieldnames=[f[0] for f in cursor.description]
# if a dumpfile was specified, open it
if dumpfile:
try:
f = open(dumpfile, "w")
except EnvironmentError as err:
print("OS error opening file: {0}".format(err))
sys.exit(1)
# output a first-line colummn name header
if dumpfile:
f.write(",".join(fieldnames).upper() + "\n")
else:
print(",".join(fieldnames).upper())
for row in cursor.fetchall():
line = []
# need to comma separate strings
for key in fieldnames:
val = row[key]
# imperfect method to detect numerics and do quoting
# - any alpha's or punctuation? double-quote accordingly
punc = string.punctuation.replace(".","") # remove the "." to avoide quoting floats
if any(c.isalpha() for c in str(val)) or containsAny(val, punc):
val = '"' + val + '"'
line.append(val)
linestr = ",".join(map(str,line)) # comma separated line of text
if dumpfile:
f.write(linestr + "\n")
else:
print(linestr)