【问题标题】:error using MySQLdb使用 MySQLdb 时出错
【发布时间】:2015-10-09 11:13:45
【问题描述】:

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

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)

请给点建议

【问题讨论】:

  • 请不要从您的问题中编辑错误。您的问题现在无法使用。

标签: python apache localhost mysql-python


【解决方案1】:

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

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

【讨论】:

  • 在 Traceback 中出现错误(最近一次调用最后一次):文件“”,第 4 行,在 密码='tiger')文件“C:\Python34\lib\ site-packages\MySQLdb_init_.py”,第 81 行,在 Connect 返回 Connection(*args, **kwargs) 文件“C:\Python34\lib\site-packages\MySQLdb\connections.py ",第 204 行,在 init 中 super(Connection, self).__init__(*args, **kwargs2) TypeError: 'password' is an invalid keyword argument for this function
  • 这是“密码”,而不是“密码”。
  • 哦,是的。我通常使用 mysql 网页上的 mysql 连接器,在那里你使用密码dev.mysql.com/doc/connector-python/en/…
猜你喜欢
  • 2012-02-05
  • 2011-06-30
  • 1970-01-01
  • 2013-09-04
  • 2016-03-05
  • 2019-01-18
  • 1970-01-01
  • 2013-09-09
相关资源
最近更新 更多