-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddl.py
55 lines (45 loc) · 1.22 KB
/
ddl.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
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
import mysql.connector
from mysql.connector import errorcode
TABLES={}
TABLES['vendor'] = (
"CREATE TABLE `vendor` ("
"`name` VARCHAR(60) NOT NULL KEY,"
"`alias` VARCHAR(60) ,"
"`category` VARCHAR(60) )"
)
TABLES['budget'] = (
"CREATE TABLE `budget` ("
"`month` TINYINT NOT NULL,"
"`year` SMALLINT NOT NULL)"
)
TABLES['transaction'] = (
"CREATE TABLE `transaction` ("
"`idx` SERIAL,"
"`date` DATE,"
"`amount` DECIMAL(7,2) NOT NULL,"
"`vendor` VARCHAR(60),"
"`alias` VARCHAR(60),"
"`category` VARCHAR(60) )"
)
config = {
'user':'user',
'password':'pword',
'database':'cash'
}
sql = mysql.connector.connect(**config)
cursor = sql.cursor()
for name,ddl in TABLES.items():
try:
print("Creating table {}: ".format(name), end='')
cursor.execute(ddl)
except mysql.connector.Error as err:
print('')
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("already exists.")
else:
print(err.msg)
else:
print("OK")
cursor.close()
sql.close()