0

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.

Emil Rowland
  • 538
  • 5
  • 25
  • 1
    If you're getting an exception before the `cursor = conn.cursor()` line, then the cursor.close() will likely fail ... – Bret Jun 15 '16 at 13:51
  • Okej so must likely the `conn = MySQLConnection(**dbconfig)` is wrong. – Emil Rowland Jun 15 '16 at 13:54
  • I got a new error after deleting the try statement. `mysql not found in the config.ini file` – Emil Rowland Jun 15 '16 at 14:04
  • Found the error know I was calling the python script when I'am outside the folder. But is there a way to avoid this error so I can call the python script outside the folder? – Emil Rowland Jun 15 '16 at 14:09
  • Try `os.chdir` -- http://docs.python.org/library/os.html#os.chdir – Bret Jun 15 '16 at 14:11

2 Answers2

2

So ... after a bit of troubleshooting ...

The solution was to set the current working directory when calling the script from outside the folder in which the config file was located.

Community
  • 1
  • 1
Bret
  • 2,283
  • 4
  • 20
  • 28
1

I've ran your code from read_db_config and everything it`s okay. I think that the problem is the path of the config file. If you just from the root path of your application, I mean:

- root_dir
    - parser.py
    - config.ini

your function will work well, but when you run as an imported lib, you need to pass the whole path to the function.

>>import os
>>parser = ConfigParser()
>>parser.read(os.path.abspath('<abs_path_of_config_file>')
Ederson Badeca
  • 363
  • 1
  • 9