Skip to content

Commit edd5b93

Browse files
committed
Merge pull request #32 from d-Rickyy-b/autocreateDB
Autocreatedb
2 parents ac20374 + ebb15f7 commit edd5b93

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

database/db_wrapper.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,46 @@ class __DBwrapper(object):
1111

1212
def __init__(self):
1313
database_path = os.path.join(self.dir_path, "users.db")
14+
15+
if not os.path.exists(database_path):
16+
print("File '" + database_path + "' does not exist! Trying to create one.")
17+
try:
18+
self.create_database(database_path)
19+
except:
20+
print("An error has occured while creating the database!")
21+
1422
self.connection = sqlite3.connect(database_path)
1523
self.connection.text_factory = lambda x: str(x, 'utf-8', "ignore")
1624
self.cursor = self.connection.cursor()
1725

26+
def create_database(self, database_path):
27+
# Create database file and add admin and users table to the database
28+
open(database_path, 'a').close()
29+
30+
connection = sqlite3.connect(database_path)
31+
connection.text_factory = lambda x: str(x, 'utf-8', "ignore")
32+
cursor = connection.cursor()
33+
34+
cursor.execute("CREATE TABLE 'admins' "
35+
"('userID' INTEGER NOT NULL,"
36+
"'first_name' TEXT,"
37+
"'username' TEXT,"
38+
"PRIMARY KEY('userID'));")
39+
40+
cursor.execute("CREATE TABLE 'users'"
41+
"('userID' INTEGER NOT NULL,"
42+
"'languageID' TEXT,"
43+
"'first_name' TEXT,"
44+
"'last_name' TEXT,"
45+
"'username' TEXT,"
46+
"'gamesPlayed' INTEGER,"
47+
"'gamesWon' INTEGER,"
48+
"'gamesTie' INTEGER,"
49+
"'lastPlayed' INTEGER,"
50+
"PRIMARY KEY('userID'));")
51+
connection.commit()
52+
connection.close()
53+
1854
def get_user(self, user_id):
1955
self.cursor.execute("SELECT * FROM users WHERE userID=?;", [str(user_id)])
2056

0 commit comments

Comments
 (0)