-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
dbsetup.py
184 lines (163 loc) · 6.93 KB
/
dbsetup.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import csv
import os
from sqlite3 import connect
import Game
class dbsetup():
def __init__(self):
self.dbpath = './db/game.db'
# import and create our player database
self.gamedb = connect(self.dbpath)
self.conn = self.gamedb.cursor()
# used to delete the current database
def deletedbifexists(self):
if os.path.exists('./db/game.db'):
os.remove('./db/game.db')
def setupdb(self):
# If you set this to 1, it will print out all data as it populates the datbase.
debugging = 0
# make a database connection to the game database
if debugging:
print('connecting to database')
conn = connect('./db/game.db')
# create our cursor
if debugging:
print('creating cursor')
cur = conn.cursor()
# create our armor table in the database
if debugging:
print('creating table for armor')
cur.execute(
'''CREATE TABLE IF NOT EXISTS armor (level INTEGER, class TEXT, name TEXT, type TEXT, basedef INTEGER, durability INTEGER)''')
# insert our armor table in the database
if debugging:
print('inserting armor into database')
with open('./csv/armor.csv', 'r') as fin:
dr = csv.reader(fin)
for i in dr:
if debugging:
print('inserting ' + str(i))
cur.execute('INSERT INTO armor VALUES (?,?,?,?,?,?);', i)
if debugging:
cur.execute('SELECT * FROM armor')
rows = cur.fetchall()
for row in rows:
print('QUERY ALL: ' + str(row))
# create our enemy table in the database
if debugging:
print('creating table for enemies')
cur.execute(
'''CREATE TABLE IF NOT EXISTS enemies(level INT, firstname TEXT, middlename TEXT, lastname TEXT, attack INTEGER, xp INTEGER, gold INTEGER, hp INTEGER, def INTEGER, status TEXT)''')
# insert our enemy table in the database
if debugging:
print('inserting enemies into database')
with open('./csv/enemies.csv', 'r') as fin:
dr = csv.reader(fin)
for i in dr:
if debugging:
print('inserting ' + str(i))
cur.execute('INSERT INTO enemies VALUES (?,?,?,?,?,?,?,?,?,?);', i)
if debugging:
cur.execute('SELECT * FROM enemies')
rows = cur.fetchall()
for row in rows:
print('QUERY ALL: ' + str(row))
# create our items table in the database
if debugging:
print('creating table for items')
cur.execute(
'''CREATE TABLE IF NOT EXISTS items(level INT, grade INT,name TEXT,effect INT,value INT)''')
# insert our items table in the database
if debugging:
print('inserting items into database')
with open('./csv/items.csv', 'r') as fin:
dr = csv.reader(fin)
for i in dr:
if debugging:
print('inserting ' + str(i))
cur.execute('INSERT INTO items VALUES (?,?,?,?,?);', i)
if debugging:
cur.execute('SELECT * FROM items')
rows = cur.fetchall()
for row in rows:
print('QUERY ALL: ' + str(row))
# create our levelnotes table in the database
if debugging:
print('creating table for levelnotes')
cur.execute(
'''CREATE TABLE IF NOT EXISTS levelnotes(Level INT,HP INT,ATK INT,DEF INT,xptonextlevel INT, dodge INT )''')
# insert our levelnotes table in the database
if debugging:
print('inserting levelnotes into database')
with open('./csv/levelnotes.csv', 'r') as fin:
dr = csv.reader(fin)
for i in dr:
if debugging:
print('inserting ' + str(i))
cur.execute('INSERT INTO levelnotes VALUES (?,?,?,?,?,?);', i)
if debugging:
cur.execute('SELECT * FROM levelnotes')
rows = cur.fetchall()
for row in rows:
print('QUERY ALL: ' + str(row))
# create our shields table in the database
if debugging:
print('creating table for shields')
cur.execute(
'''CREATE TABLE IF NOT EXISTS shields (level INT,class TEXT,name TEXT,type TEXT,basedef INT,durability INT)''')
# insert our shields table in the database
if debugging:
print('inserting shields into database')
with open('./csv/shields.csv', 'r') as fin:
dr = csv.reader(fin)
for i in dr:
if debugging:
print('inserting ' + str(i))
cur.execute('INSERT INTO shields VALUES (?,?,?,?,?,?);', i)
if debugging:
cur.execute('SELECT * FROM shields')
rows = cur.fetchall()
for row in rows:
print('QUERY ALL: ' + str(row))
# create our weapons table in the database
if debugging:
print('creating table for weapons')
cur.execute(
'''CREATE TABLE IF NOT EXISTS weapons ( level INTEGER ,class TEXT ,name TEXT ,type TEXT,baseattack INTEGER ,durability INTEGER ,power TEXT)''')
# insert our weapons table in the database
if debugging:
print('inserting weapons into database')
with open('./csv/weapons.csv', 'r') as fin:
dr = csv.reader(fin)
for i in dr:
if debugging:
print('inserting ' + str(i))
cur.execute('INSERT INTO weapons VALUES (?,?,?,?,?,?,?);', i)
if debugging:
cur.execute('SELECT * FROM weapons')
rows = cur.fetchall()
for row in rows:
print('QUERY ALL: ' + str(row))
# create our riddles table in the database
if debugging:
print('creating table for riddles')
cur.execute(
'''CREATE TABLE IF NOT EXISTS riddles (question TEXT ,answer TEXT)''')
# insert our riddles table in the database
if debugging:
print('inserting riddles into database')
with open('./csv/riddles.csv', 'r') as fin:
dr = csv.reader(fin)
for i in dr:
if debugging:
print('inserting ' + str(i))
cur.execute('INSERT INTO riddles VALUES (?,?);', i)
if debugging:
cur.execute('SELECT * FROM riddles')
rows = cur.fetchall()
for row in rows:
print('QUERY ALL: ' + str(row))
# commit the changes
conn.commit()
# close the database connection to let other operations use it
conn.close()
Game.centerprint('...Have fun')