总 Python 新手在这里。
我正在关注此页面的教程。但是当我运行我的代码时,我在控制台中收到以下错误。
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
搜索我发现的一些解决方案需要包含 unix_socket 路径,正如您所看到的那样。
我也在使用XAMPP,我使用的套接字路径是 XAMPP 的 my.cnf 文件中指定的路径。我正在运行 MySQL,端口是 3306。
请帮助这个菜鸟。
Python代码:
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()