File size: 1,662 Bytes
6bf71bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv
load_dotenv()
import os

import mysql.connector
from mysql.connector import errorcode

config={
    'host':os.environ.get("HOSTNAME"),
    'user':os.environ.get("UID"),
    'password':os.environ.get("PASSWORD"),
    'database':os.environ.get("DATABASE")
}

print(config)

try:
    cnx = mysql.connector.connect(**config)
    print("Connection established")
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with username or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)
else:
    cursor = cnx.cursor()

    cursor.execute("DROP TABLE IF EXISTS api_key")
    cursor.execute("DROP TABLE IF EXISTS auth")
    cursor.execute("CREATE TABLE IF NOT EXISTS auth(username VARCHAR(15) PRIMARY KEY, password TEXT, email VARCHAR(50))")
    cursor.execute("CREATE TABLE IF NOT EXISTS api_key(username VARCHAR(15),apikey TEXT, FOREIGN KEY (username) REFERENCES auth(username))")

    QUERY = ('INSERT INTO {coll_name} '
                    '(username, password, email) '
                    'VALUES '
                    '(%s, %s, %s)').format(coll_name="auth")

    testlist=[("test2","test2","[email protected]"),("test1","test1","[email protected]")]
    cursor.executemany(QUERY, testlist)

    QUERY = ('SELECT {cols} FROM {table_name} WHERE email="[email protected]"').format(cols="*", table_name="auth")
    cursor.execute(QUERY)
    for i in cursor.fetchall():
        print(i)




    cnx.commit()
    cursor.close()
    cnx.close()
# from jose import jwt
# print(jwt.encode("bruhh"))