Python MySQLdb连接导入错误

时间:2016-02-25 22:30:06

标签: python mysql dbconnection

我正在尝试使用以下代码与db连接:

import MySQLdb

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
                     user="root",         # your username
                     passwd="root",  # your password
                     db="test101")        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

# Use all the SQL you like
cur.execute("SELECT * FROM test1")

# print all the first cell of all the rows
for row in cur.fetchall():
    print row[0]

db.close()

但是,我在控制台上收到以下错误消息:

Traceback (most recent call last):
  File "C:\Users\JRambo\workspace\DBConnection\src\DBConnection.py", line 6, in <module>
    import MySQLdb
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
    import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.

我一丝不苟地遵循了这些步骤。 How do I connect to a MySQL Database in Python?

1 个答案:

答案 0 :(得分:1)

您可能想验证您是否拥有正确的Python位和正确的位MySQLdb。如果你有32位Python和64位MySQLdb它就不会工作。我有一个类似的问题与相同的Traceback错误,当我安装每个应用程序的正确位类型,宾果游戏!希望这有帮助!