Search code examples
pythonmysqldebuggingattributeerror

Where to define in Python class, mysql connection?


So, I just got started today with python. I installed the python-mysql.connector so I can start writing some queries and within python work with the result, and then output some data to an lcd screen.

Right know this is what I have....

#!/usr/bin/python

import Adafruit_CharLCD
import mysql.connector

class LCDTests:

    def __init__(self):

        self.lcd = Adafruit_CharLCD;
        #self.lcd.begin(16,1);

        try:
            cnx = mysql.connector(user = '', password='' database='', host='')
        except mysql.connector.Error, e:
            try:
                print "MySQL Error [%d]: %s" % (e.args[0], e.args[1])
            except IndexError:
                print "MySQL Error: %s" % str(e)            
        else:
                cnx.close()

    def run(self):
            print('alex')
            #lcd.message('loading...');

test = LCDTests()
test.run()

But when ever I run ./test.py I keep getting the following output:

Traceback (most recent call last):
  File "./test.py", line 31, in <module>
    test = LCDTests()
  File "./test.py", line 16, in __init__
    cnx = mysql.connector(user = '...', password='...', database='...', host='...')
TypeError: 'module' object is not callable

Nothing what I found, while searching for the issue, really helped me fixing the issue... any ideas where the issue might be?


Solution

  • mysql.connector.connect(...) not mysql.connector(...)

    module docs will probably be a good resource for you.