2011-05-24 113 views
1

我的config.ini:Python和MySQL连接问题(MySQLdb的API)

[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

如果有人可以帮助检测和纠正错误将不胜感激。 在此先感谢。

+1

所以......你想通过TCP/IP或通过Unix域套接字连接吗? – 2011-05-24 17:08:00

+0

良好的捕获,两者都不需要。但是如果连接失败,会不会__init __()引发异常? – 2011-05-24 17:09:07

回答

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

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

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

这很疯狂!现在“TypeError:connect()参数2必须是字符串,而不是int” – x13 2011-05-24 20:00:22

0

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