Alex
Alex

Reputation: 7688

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?

Upvotes: 1

Views: 2437

Answers (3)

cmd
cmd

Reputation: 5830

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

module docs will probably be a good resource for you.

Upvotes: 4

R A
R A

Reputation: 162

You have a typo. Currently you are referencing the module mysql.connector not the Connect class in the module. try

mysql.connector.Connect(...)

Upvotes: 0

Thomas Orozco
Thomas Orozco

Reputation: 55207

The proper syntax is mysql.connector.connect(user, password, host, database).

Upvotes: 2

Related Questions