-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
58 lines (43 loc) · 1.55 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
'''
Created on Sep 22, 2012
Handles all data access for application
@author: creed
'''
import MySQLdb
class Data:
def __init__(self):
try:
self.conn = MySQLdb.connect(host="localhost",
user="testPythonApp",
passwd="testPy",
db="testPythonAppDB"
)
self.cursor = self.conn.cursor()
except MySQLdb.Error, e:
print "MySQLdb error %d: %s" % (e.args[0], e.args[1])
return 0
def insertData(self, table, dataArray):
query = self.buildInsertQuery(table, dataArray)
try:
self.cursor.execute(query)
self.conn.commit()
except Exception, e:
self.conn.rollback()
print "Unable to insert" + e
def buildInsertQuery(self, table, dataArray):
base = "INSERT INTO " + table + " ("
tableFieldNames = dataArray[0]
for titleRow in tableFieldNames:
if tableFieldNames[-1] == titleRow:
base = base + titleRow
else:
base = base + titleRow + ","
del dataArray[0]
base = base + ") VALUES "
for line in dataArray:
if line[0] != "Season" and line[0] != "Career":
base = base + "("
for data in line[:-1]:
base = base + str(data) + ','
base = base + str(data) + ")"
return base