【问题标题】:Python and MySQL connection problems (mysqldb api)Python 和 MySQL 连接问题(mysqldb api)
【发布时间】:2011-09-01 03:02:14
【问题描述】:

我有 config.ini:

[mysql]
host=localhost
port=3306
user=root
passwd=abcdefgh
db=testdb
unix_socket=/opt/lampp/var/mysql/mysql.sock

我有这门课:

#!/usr/bin/python
import MySQLdb,ConfigParser
config = ConfigParser.ConfigParser()
config.read("config.ini")

class MySQL( object ):

    def __init__( self ):
        self.host   = config.get("mysql","host")
        self.port   = config.get("mysql","port")
        self.user   = config.get("mysql","user")
        self.passwd = config.get("mysql","passwd")
        self.db     = config.get("mysql","db")
        self.unix_socket = config.get("mysql","unix_socket")

        self.conn   = MySQLdb.Connect(self.host,
                                      self.port,
                                      self.user,
                                      self.passwd,
                                      self.db,
                                      self.unix_socket)

        self.cursor = self.conn.cursor ( MySQLdb.cursors.DictCursor )

    def __del__( self ):
        self.cursor.close()
        self.conn.close()

还有这个:

#!/usr/bin/env python  
from mysql import MySQL

class Incident( MySQL ):

    def getIncidents( self ):
        self.cursor.execute("""*VALID QUERY*""")
        return self.cursor.fetchall()

最后是这个:

import subprocess, os, alarm
from Queue import Queue
from incident_model import Incident

fileQueue = Queue()

def enumerateFilesPath():
  global fileQueue
  incident = Incident()
  incidents = incident.getIncidents()
  for i in incidents:
    fileQueue.put("MD5")

def main():
    global fileQueue
    enumerateFilesPath()

输出:

回溯(最近一次通话最后一次):
文件“./mwmonitor.py”,第 202 行,在

main() 文件“./mwmonitor.py”,第 184 行,在 main
enumerateFilesPath() 文件“./mwmonitor.py”,第 86 行,在
枚举文件路径
事件 = Incident() 文件“/usr/share/mwanalysis/core/mysql.py”,
第 23 行,在 init
self.unix_socket) 文件“/usr/lib/pymodules/python2.6/MySQLdb/init.py”,
第 81 行,在 Connect
返回连接(*args,**kwargs)文件
"/usr/lib/pymodules/python2.6/MySQLdb/connections.py",
第 170 行,在 init
super(Connection, self).init(*args, **kwargs2)
类型错误:需要一个整数
异常 AttributeError:“'事件'
对象在
中没有属性“光标” 0xa03d46c>> 忽略

如果有人可以帮助检测和纠正错误,将不胜感激。 提前致谢。

【问题讨论】:

  • 那么...您想通过 TCP/IP 还是通过 Unix 域套接字连接?
  • 很好,两者都不需要。但是如果连接失败,__init__() 不会引发异常吗?

标签: python mysql database-connection mysql-python


【解决方案1】:

您的__del__ 方法引起了混乱。具体来说,它指的是self.cursorself.conn,例如,如果MySQLdb.Connect 引发异常(这似乎是发生的情况),则它们可能永远不会被创建。

我建议你修改你的类如下:

class MySQL( object ):

    def __init__( self ):

        self.conn   = None
        self.cursor = None

        self.host   = config.get("mysql","host")
        self.port   = config.get("mysql","port")
        self.user   = config.get("mysql","user")
        self.passwd = config.get("mysql","passwd")
        self.db     = config.get("mysql","db")
        self.unix_socket = config.get("mysql","unix_socket")

        self.conn   = MySQLdb.Connect(self.host,
                                      self.port,
                                      self.user,
                                      self.passwd,
                                      self.db,
                                      self.unix_socket)

        self.cursor = self.conn.cursor ( MySQLdb.cursors.DictCursor )

    def __del__( self ):
        if self.cursor is not None:
            self.cursor.close()
        if self.conn is not None:
            self.conn.close()

这不会解决问题,但应该提供更好的诊断。

现在谈谈您遇到的实际问题。我强烈怀疑您以错误的顺序向Connect 提供参数,或者类型不太正确,或者类似的东西。引用 Connection.__init__ 的文档字符串:

    Create a connection to the database. It is strongly recommended
    that you only use keyword parameters. Consult the MySQL C API
    documentation for more information.

    host
      string, host to connect

    user
      string, user to connect as

    passwd
      string, password to use

    db
      string, database to use

    port
      integer, TCP/IP port to connect to

    unix_socket
      string, location of unix_socket to use

    ...

“强烈建议您只使用关键字参数。”我建议您在调用MySQLdb.Connect 时这样做。另外,请确保 portint 而不是字符串。

【讨论】:

    【解决方案2】:

    我怀疑它期望 port 是一个整数而不是一个字符串。试试:

    self.port   = int(config.get("mysql","port"))
    

    【讨论】:

      【解决方案3】:

      我不确定这是否是连接错误。您是否检查过 event_model 的类型? TypeError: an integer is required Exception AttributeError: "'Incident'object has no attribute 'cursor'" in

      【讨论】:

        猜你喜欢
        • 2010-10-20
        • 1970-01-01
        • 1970-01-01
        • 2017-08-23
        • 1970-01-01
        • 2011-03-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多