Skip to content

Commit

Permalink
Merge pull request #70 from Nightem/async-database
Browse files Browse the repository at this point in the history
Change database to be async
  • Loading branch information
MiataBoy authored Apr 11, 2024
2 parents df2f04c + abbebf7 commit 65ebabe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
async def on_connect():
print('Connected to Discord!')
cursor = await mysql_login()
database = cursor.cursor()
database.execute("CREATE TABLE IF NOT EXISTS server (guild_id VARCHAR(255) PRIMARY KEY, server_ip TEXT NOT NULL)")
database.execute("CREATE TABLE IF NOT EXISTS blacklist (guild_id VARCHAR(21) PRIMARY KEY, reason TEXT NOT NULL)")
database.close()
database = await cursor.cursor()
await database.execute("CREATE TABLE IF NOT EXISTS server (guild_id VARCHAR(255) PRIMARY KEY, server_ip TEXT NOT NULL)")
await database.execute("CREATE TABLE IF NOT EXISTS blacklist (guild_id VARCHAR(21) PRIMARY KEY, reason TEXT NOT NULL)")
await database.close()


@bot.listen()
Expand Down
26 changes: 14 additions & 12 deletions utilities/database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from mysql import connector as mysql
from mysql.connector.aio import connect

from utilities.data import get_data


Expand All @@ -9,7 +10,7 @@ async def mysql_login():
"""
database = get_data()['Database']

return mysql.connect(
return await connect(
host=database['Host'],
user=database['User'],
password=database['Password'],
Expand All @@ -24,14 +25,15 @@ async def selector(query: str, variables: list) -> tuple:
:return: The result of the query. If there is no result, it will return False.
"""
cursor = await mysql_login()
database = cursor.cursor()
database.execute(query, variables)
database = await cursor.cursor()
await database.execute(query, variables)
try:
result = database.fetchall()[0]
result = await database.fetchall()
result = result[0]
except IndexError:
return ()
database.close()
cursor.close()
await database.close()
await cursor.close()
return result


Expand All @@ -43,8 +45,8 @@ async def modifier(query: str, variables: list) -> None:
:return: None
"""
cursor = await mysql_login()
database = cursor.cursor()
database.execute(query, variables)
cursor.commit()
database.close()
cursor.close()
database = await cursor.cursor()
await database.execute(query, variables)
await cursor.commit()
await database.close()
await cursor.close()

0 comments on commit 65ebabe

Please sign in to comment.