Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python类中的何处定义mysql连接?_Python_Mysql_Debugging_Attributeerror - Fatal编程技术网

在Python类中的何处定义mysql连接?

在Python类中的何处定义mysql连接?,python,mysql,debugging,attributeerror,Python,Mysql,Debugging,Attributeerror,所以,我今天刚开始学习python。我安装了pythonmysql.connector,因此我可以开始编写一些查询,并在python中处理结果,然后将一些数据输出到lcd屏幕 好的,我知道这就是我所拥有的 #!/usr/bin/python import Adafruit_CharLCD import mysql.connector class LCDTests: def __init__(self): self.lcd = Adafruit_CharLCD;

所以,我今天刚开始学习python。我安装了
pythonmysql.connector
,因此我可以开始编写一些查询,并在python中处理结果,然后将一些数据输出到lcd屏幕

好的,我知道这就是我所拥有的

#!/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()
但是每当我运行
/test.py
时,我总是得到以下输出:

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
回溯(最近一次呼叫最后一次):
文件“/test.py”,第31行,在
测试=LCDTests()
文件“/test.py”,第16行,在__
cnx=mysql.connector(用户=“…”,密码=“…”,数据库=“…”,主机=“…”)
TypeError:“模块”对象不可调用

我在搜索问题时发现的一切都没有真正帮助我解决问题。。。有什么问题吗?

mysql.connector.connect(用户、密码、主机、数据库)
mysql.connector.connect(…)
不是
mysql.connector(…)


这可能是您的一个好资源。

您有一个打字错误。当前您正在引用模块mysql.connector,而不是模块中的Connect类。试一试

mysql.connector.Connect(...)