Spaces:
Runtime error
Runtime error
Commit
·
6bf71bb
1
Parent(s):
5d8f196
Add init scripts
Browse files- scripts/test.py +55 -0
scripts/test.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
load_dotenv()
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
import mysql.connector
|
| 6 |
+
from mysql.connector import errorcode
|
| 7 |
+
|
| 8 |
+
config={
|
| 9 |
+
'host':os.environ.get("HOSTNAME"),
|
| 10 |
+
'user':os.environ.get("UID"),
|
| 11 |
+
'password':os.environ.get("PASSWORD"),
|
| 12 |
+
'database':os.environ.get("DATABASE")
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
print(config)
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
cnx = mysql.connector.connect(**config)
|
| 19 |
+
print("Connection established")
|
| 20 |
+
except mysql.connector.Error as err:
|
| 21 |
+
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
|
| 22 |
+
print("Something is wrong with username or password")
|
| 23 |
+
elif err.errno == errorcode.ER_BAD_DB_ERROR:
|
| 24 |
+
print("Database does not exist")
|
| 25 |
+
else:
|
| 26 |
+
print(err)
|
| 27 |
+
else:
|
| 28 |
+
cursor = cnx.cursor()
|
| 29 |
+
|
| 30 |
+
cursor.execute("DROP TABLE IF EXISTS api_key")
|
| 31 |
+
cursor.execute("DROP TABLE IF EXISTS auth")
|
| 32 |
+
cursor.execute("CREATE TABLE IF NOT EXISTS auth(username VARCHAR(15) PRIMARY KEY, password TEXT, email VARCHAR(50))")
|
| 33 |
+
cursor.execute("CREATE TABLE IF NOT EXISTS api_key(username VARCHAR(15),apikey TEXT, FOREIGN KEY (username) REFERENCES auth(username))")
|
| 34 |
+
|
| 35 |
+
QUERY = ('INSERT INTO {coll_name} '
|
| 36 |
+
'(username, password, email) '
|
| 37 |
+
'VALUES '
|
| 38 |
+
'(%s, %s, %s)').format(coll_name="auth")
|
| 39 |
+
|
| 40 |
+
testlist=[("test2","test2","test2@test.com"),("test1","test1","test1@test1.com")]
|
| 41 |
+
cursor.executemany(QUERY, testlist)
|
| 42 |
+
|
| 43 |
+
QUERY = ('SELECT {cols} FROM {table_name} WHERE email="test2@test.com"').format(cols="*", table_name="auth")
|
| 44 |
+
cursor.execute(QUERY)
|
| 45 |
+
for i in cursor.fetchall():
|
| 46 |
+
print(i)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
cnx.commit()
|
| 52 |
+
cursor.close()
|
| 53 |
+
cnx.close()
|
| 54 |
+
# from jose import jwt
|
| 55 |
+
# print(jwt.encode("bruhh"))
|