-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb_connection.py
executable file
·47 lines (36 loc) · 1.34 KB
/
db_connection.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
import os
import pymysql
import sqlalchemy
import urllib.parse
from configparser import ConfigParser
config = ConfigParser()
config.read('credentials.ini')
host = config['mysql']['host']
port = config['mysql']['port']
username = config['mysql']['username']
password = config['mysql']['password']
database = config['mysql']['database']
username = urllib.parse.quote_plus(username)
password = urllib.parse.quote_plus(password)
def get_mysql_connection():
try:
return sqlalchemy.create_engine(f'mysql+pymysql://{username}:{password}@{host}:{port}/{database}')
except Exception as e:
print("Something went wrong:", e)
def get_mongo_connection():
try:
return sqlalchemy.create_engine(f'mysql+pymysql://{username}:{password}@{host}:{port}/{database}')
except Exception as e:
print("Something went wrong:", e)
def get_mysql_pymysql_connection():
try:
return pymysql.connect(host='localhost',
user='root',
password='',
db='tutorfall2016',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
except Exception as e:
print("Something went wrong:", e)
if __name__ == '__main__':
get_mysql_connection()