使用MySQLdb时出错

时间:2015-07-20 10:54:32

标签: python apache localhost mysql-python

我是python的新手,我有简单的代码使用MySQLdb和在localhost中运行的Python3.4连接数据库。

Python代码:

#!c:/Python34/python.exe -u
import cgitb ,cgi
import sys
import MySQLdb
conn = MySQLdb.connect(host='localhost',port=3306,
                                       db='example2',
                                       user='root',
                                       password='tiger')
cursor = conn.cursor()
if conn:
   print("sucess")

但在执行代码时遇到错误 错误:

Traceback (most recent call last):
  File "<pyshell#27>", line 4, in <module>
    password='tiger')
  File "C:\Python34\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python34\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required (got type str)

请提出一些建议

1 个答案:

答案 0 :(得分:0)

您需要从端口删除单引号。

conn = MySQLdb.connect(host='localhost',
                       port=3306,
                       database='example2',
                       user='root',
                       password='tiger')