1

我有 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 行,在
enumerateFilesPath
事件 = Incident() 文件“/usr/share/mwanalysis/core/mysql.py”,
第 23 行,在init
self.unix_socket) 文件“/usr/lib/pymodules/python2.6/MySQLdb/ init .py”,
第 81 行,在 Connect
返回 Connection(*args, **kwargs) 文件
“/usr/lib/pymodules/python2.6/MySQLdb/connections.py”,
第 170 行,在init
super(Connection, self )。初始化(*args, **kwargs2)
TypeError:需要一个整数
异常 AttributeError:“'事件'对象在 0xa03d46c>>
中没有属性'光标'”

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

4

3 回答 3

2

你的__del__方法引起了混乱。具体来说,它指的是如果引发异常(这似乎是发生的情况)self.cursor,则self.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。另外,请确保它port是一个int而不是一个字符串。

于 2011-05-24T20:26:46.967 回答
1

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

self.port   = int(config.get("mysql","port"))
于 2011-05-24T17:09:43.863 回答
0

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

于 2011-05-24T17:10:43.620 回答