0

I am trying to connect with db using the following code:

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()

However, I am getting the following error message on the console:

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.

I have followed the steps meticulously. How do I connect to a MySQL Database in Python?

1 Answer 1

1

You might want to verify that you have the correct bit Python and correct bit MySQLdb. If you have 32 bit Python and 64 bit MySQLdb it won't work. I had a similar problem with the same Traceback error and when I installed the correct bit type of each application, bingo! Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!! I just realized that
Great! Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.