-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connector.py
More file actions
49 lines (37 loc) · 1.71 KB
/
db_connector.py
File metadata and controls
49 lines (37 loc) · 1.71 KB
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
import MySQLdb as mariadb
import os
# get environment variables from heroku to connect to the database
db_url = os.environ['CLEARDB_DATABASE_URL']
db_url = db_url.replace('/',' ').replace(':',' ').replace('@',' ').replace('?',' ').split()
user = db_url[1]
passwd = db_url[2]
host = db_url[3]
db = db_url[4]
def connect_to_database(host = host, user = user, passwd = passwd, db = db):
'''
connects to a database and returns a database objects
'''
db_connection = mariadb.connect(host,user,passwd,db)
return db_connection
def execute_query(db_connection = None, query = None, query_params = ()):
'''
executes a given SQL query on the given db connection and returns a Cursor object
db_connection: a MySQLdb connection object created by connect_to_database()
query: string containing SQL query
returns: A Cursor object as specified at https://www.python.org/dev/peps/pep-0249/#cursor-objects.
You need to run .fetchall() or .fetchone() on that object to actually acccess the results.
'''
if db_connection is None:
print("No connection to the database found! Have you called connect_to_database() first?")
return None
if query is None or len(query.strip()) == 0:
print("query is empty! Please pass a SQL query in query")
return None
print("Executing %s with %s" % (query, query_params));
# Create a cursor to execute query. Why? Because apparently they optimize execution by retaining a reference according to PEP0249
cursor = db_connection.cursor()
cursor.execute(query, query_params)
# this will actually commit any changes to the database. without this no
# changes will be committed!
db_connection.commit();
return cursor