Python 和 MySQL 连接问题(mysqldb api)

标签 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()

输出:

Traceback (most recent call last):
File "./mwmonitor.py", line 202, in

main() File "./mwmonitor.py", line 184, in main
enumerateFilesPath() File "./mwmonitor.py", line 86, in
enumerateFilesPath
incident = Incident() File "/usr/share/mwanalysis/core/mysql.py",
line 23, in init
self.unix_socket) File "/usr/lib/pymodules/python2.6/MySQLdb/init.py",
line 81, in Connect
return Connection(*args, **kwargs) File
"/usr/lib/pymodules/python2.6/MySQLdb/connections.py",
line 170, in init
super(Connection, self).init(*args, **kwargs2)
TypeError: an integer is required
Exception AttributeError: "'Incident'
object has no attribute 'cursor'" in
0xa03d46c>> ignored

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

最佳答案

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

关于Python 和 MySQL 连接问题(mysqldb api),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6114064/

相关文章:

python - 使用 python opencv 进行 Blob 检测

python - 使用值作为断点来分解列表?

mysql - SQL查询速度

mysql - 在sql中按功能分组

c#-4.0 - 如何处理数据库连接的并行执行 C#

postgresql - 空闲 PostgreSQL 连接是否有超时?

python - SQL Alchemy 使用带有 bindparam 的字典列表更新表不工作

MySQL 和 MySQL2 gem 在主机上构建,但不通过 Capistrano 或 SSH 构建

java - 即使没有事务,在 jboss 中检索到多个数据库的连接时出错

python - 如何记录到文件和标准输出