Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TradingBot/database.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
58 lines (48 sloc)
1.47 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Creates connection to database and funtion for sending queries to database """ | |
import mysql.connector | |
from mysql.connector import Error | |
# Create connection to database | |
config = {'user' : 'root', | |
'password' : 'localhost', | |
'host' : '127.0.0.1', | |
'database' : 'trading_bot'} | |
def run_value_query(query, values): | |
try: | |
cnx = mysql.connector.connect(**config) | |
cursor = cnx.cursor() | |
cursor.execute(query, (values,)) | |
data = cursor.fetchall() | |
cnx.close() | |
return data | |
except Error as err: | |
print(f"Error: '{err}'") | |
def run_multiple_value_query(query, values): | |
try: | |
cnx = mysql.connector.connect(**config) | |
cursor = cnx.cursor() | |
cursor.execute(query, values) | |
data = cursor.fetchall() | |
cnx.close() | |
return data | |
except Error as err: | |
print(f"Error: '{err}'") | |
def run_all_query(query): | |
try: | |
cnx = mysql.connector.connect(**config) | |
cursor = cnx.cursor() | |
cursor.execute(query) | |
data = cursor.fetchall() | |
cnx.close() | |
return data | |
except Error as err: | |
print(f"Error: '{err}'") | |
def run_alter_query(query, values): | |
try: | |
cnx = mysql.connector.connect(**config) | |
cursor = cnx.cursor() | |
cursor.execute(query, values) | |
cnx.commit() | |
cnx.close() | |
return(query, " was completed successfully") | |
except Error as err: | |
print(f"Error: '{err}'") |