I have used what we learnt in my lesson and tailored it to my file and database but it seems to keep coming up with errors no matter what I do. I have multiple files being imported with defined functions and another with the MySQL login.
I want to connect my database as my homework is creating a basic api and so this is a pretty crucial step! Any help would be greatly appreciated or a new plan completely is also welcome!
from flask import Flask, jsonify, request
from config import USER, HOST, PASSWORD
from utils import _connect_to_db, get_all_records, main
from pprint import pprint
import mysql.connector
connection = (mysql.connector.connect
(host='localhost',
database='animals_app',
user='root',
password='password'))
if connection.is_connected(): print("Connection Successful")
class DbConnectionError(Exception):
pass
# get info from db
def get_all_records():
db_name = 'animals_app'
db_name = 'animals_app'
# connect to the db
db_connection = _connect_to_db(db_name)
# connect cursor object to the db
try:
cursor = db_connection.cursor()
print('Successfully connected to the DB: %s' % db_name)
# query statement
query = """SELECT * FROM animals_app"""
# execute the query
cursor.execute(query)
# fetch result
result = cursor.fetchall()
# iterate and print each row
for i in result:
print(i)
# end of queries, close cursor connection
cursor.close()
except Exception:
raise DbConnectionError('Oops! Failed to read data from DB')
finally:
if db_connection:
db_connection.close()
print('DB connection is closed')
if __name__ == '__main__':
main()
Gracie Fenemer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.