2011-05-24 116 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