-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.py
41 lines (33 loc) · 876 Bytes
/
cursor.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
#!/usr/bin/python3
"""
Author: Konstantinos Liosis
File: cursor.py
Desc: Get a cursor to execute SQL statements
"""
import configparser
import mysql.connector as mysql
config = configparser.ConfigParser()
config.read('config.ini')
HOST = config['HOST']['def']
USER = config['USERS']['1']
PASS = config['PASS']['1']
def get_cursor(db=None):
"""
Create an instance of 'cursor' class to execute SQL statements
:param db: Database name (None when creating a DB)
:return: The cursor object or None
"""
try:
db_conf = {
"host": HOST,
"user": USER,
"password": PASS
}
if db:
db_conf["database"] = db
db_conn = mysql.connect(**db_conf)
cursor = db_conn.cursor()
return cursor
except Exception as e:
print(e)
return None