Python和MySQL连接问题(mysqldb api)

时间:2011-05-24 16:59:36

标签: python mysql database-connection mysql-python

我有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行,在主体中       enumerateFilesPath()文件“./mwmonitor.py”,第86行,在中   enumerateFilesPath
      事件=事件()文件“/usr/share/mwanalysis/core/mysql.py”,
  第23行,在 init 中       self.unix_socket)文件“/usr/lib/pymodules/python2.6/MySQLdb/init.py”,
  第81行,在连接中       return Connection(* args,** kwargs)文件
  “/usr/lib/pymodules/python2.6/MySQLdb/connections.py”,
  第170行,在 init 中       super(Connection,self)。 init (* args,** kwargs2)
  TypeError:需要一个整数
  异常AttributeError:“'事件'
  对象在中没有属性'cursor'“         0xa03d46c>>忽略了

如果有人可以帮助检测并纠正错误,那将非常感激。 提前谢谢。

3 个答案:

答案 0 :(得分:2)

您的__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而不是字符串。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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