0

Total Python newb here.

I'm following the tutorial from this page. But when I run my code I get the following error in the console.

Traceback (most recent call last):
  File "/Users/tejenshrestha/Documents/ProjectAccess/access/trunk/analysis/src/json_xml/Database.py", line 37, in <module>
    con = MySQLdb.connect(socket, dbhost, dbuser, dbpass, dbname);
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required

Searching some solutions I found required that the unix_socket path to be include, which I did as you can see.

I'm also using XAMPP and the socket path I'm using is the one specified in XAMPP's my.cnf file. I have MySQL running and the port it is 3306.

Please help out this noob.

The Python code:

import MySQLdb
import sys

con = None
dbhost = "localhost"
port   = "3306"
dbuser = "root"
dbpass = "password"
dbname = "name"
socket = "/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock"

try:
    con = MySQLdb.connect(socket, dbhost, dbuser, dbpass, dbname);
    cur = con.cursor()
    cur.execute("SELECT VERSION()")

    data = cur.fetchone()
    print "Database version : %s " % data

except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)

finally:    
    if con:    
        con.close()
4

1 に答える 1

4

ポート周辺から見積もりを取り、それが機能するかどうかを確認します。


<brilliant code>
port = 3306
<brilliant code>

接続関数の引数にラベルを付ける必要があると思います。ここで一番上の答えを参照してください。

PythonでMySQLデータベースに接続するにはどうすればよいですか?

于 2013-02-15T19:18:22.237 に答える