Hi I get an error when I try to connect to a mysql database.
import threading
from mysql.connector import MySQLConnection, Error
from python_mysql_dbconfig import read_db_config
class Queue (threading.Thread):
def __init__(self, threadID, lock):
threading.Thread.__init__(self)
self.threadID = threadID
self.lock = lock
def run(self):
while True:
try:
dbconfig = read_db_config()
conn = MySQLConnection(**dbconfig)
cursor = conn.cursor()
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
This is a striped part of the code but I still get the same error on the line after finally:
it says that cursor
is not defined.
The code is running in a Deamon on Ubuntu.
EDIT:
Some more information about the code
read_db_config()
from configparser import ConfigParser
def read_db_config(filename='config.ini', section='mysql'):
""" Read database configuration file and return a dictionary object
:param filename: name of the configuration file
:param section: section of database configuration
:return: a dictionary of database parameters
"""
# create parser and read ini configuration file
parser = ConfigParser()
parser.read(filename)
# get section, default to mysql
db = {}
if parser.has_section(section):
items = parser.items(section)
for item in items:
db[item[0]] = item[1]
else:
raise Exception('{0} not found in the {1} file'.format(section, filename))
return db
And the config file with false values
[mysql]
host = IP
database = database
user = user
password = password
EDIT2: Running the code without the try statement gave this error
mysql not found in the config.ini file
Can it be that my read_db_config() function can't find the config file but they are in the same folder.