I am new to Python and new to MySQL. I am trying to run an example I found on the Internet and am getting an error. I have MySQL 4.1 installed, it is running correctly as a service on WinXP Pro (I can run admin commands on a command line), my databases and tables are set up correctly. I have Python 2.3 installed and MySQLdb .92 installed. Here is the example I am trying to run:
#!/usr/bin/python
# import MySQL module
import MySQLdb
# connect
db = MySQLdb.connect(host="localhost", user="root", db="church")
# create a cursor
cursor = db.cursor()
# execute SQL statement
cursor.execute("SELECT * FROM ministers")
# get the resultset as a tuple
result = cursor.fetchall()
# iterate through resultset
for record in result:
print record[0] , "-->", record[1]
and here is my output error:
Traceback (most recent call last):
File "D:\ActiveState\Komodo-2.5\callkomodo\kdb.py", line 433, in _do_start
self.kdb.run(code_ob, locals, locals)
File "D:\ActiveState\Python22\lib\bdb.py", line 349, in run
exec cmd in globals, locals
File "D:\ActiveState\Komodo-2.5\python lessons\MySQLdb.py", line 4, in ?
import MySQLdb
File "D:\ActiveState\Komodo-2.5\python lessons\MySQLdb.py", line 7, in ?
db = MySQLdb.connect(host="localhost", user="root", db="church")
AttributeError: 'module' object has no attribute 'connect'
Any help would be greatly appreciated. I don't understand why I get this error. I have the default root account with no password.
Thanks, Jim
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hmmm, that's a little strange looking. The mysql module should be installed into site-packages for the python installation, but the last two lines of the traceback look really odd.
In MySQLdb.py there is an import of MySQLdb. Did you create a script called MySQLdb and try to use the real MySQLdb module from inside your script? If so, try changing its name to something like db.py so there aren't any namespace/import problems.
Note that your user account/password stuff have nothing to do with the error. The error is an attribute error which means something isn't defined that should be. Get that MySQLdb script renamed to something else and your problems might all go away :)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I am new to Python and new to MySQL. I am trying to run an example I found on the Internet and am getting an error. I have MySQL 4.1 installed, it is running correctly as a service on WinXP Pro (I can run admin commands on a command line), my databases and tables are set up correctly. I have Python 2.3 installed and MySQLdb .92 installed. Here is the example I am trying to run:
#!/usr/bin/python
# import MySQL module
import MySQLdb
# connect
db = MySQLdb.connect(host="localhost", user="root", db="church")
# create a cursor
cursor = db.cursor()
# execute SQL statement
cursor.execute("SELECT * FROM ministers")
# get the resultset as a tuple
result = cursor.fetchall()
# iterate through resultset
for record in result:
print record[0] , "-->", record[1]
and here is my output error:
Traceback (most recent call last):
File "D:\ActiveState\Komodo-2.5\callkomodo\kdb.py", line 433, in _do_start
self.kdb.run(code_ob, locals, locals)
File "D:\ActiveState\Python22\lib\bdb.py", line 349, in run
exec cmd in globals, locals
File "D:\ActiveState\Komodo-2.5\python lessons\MySQLdb.py", line 4, in ?
import MySQLdb
File "D:\ActiveState\Komodo-2.5\python lessons\MySQLdb.py", line 7, in ?
db = MySQLdb.connect(host="localhost", user="root", db="church")
AttributeError: 'module' object has no attribute 'connect'
Any help would be greatly appreciated. I don't understand why I get this error. I have the default root account with no password.
Thanks, Jim
Hmmm, that's a little strange looking. The mysql module should be installed into site-packages for the python installation, but the last two lines of the traceback look really odd.
In MySQLdb.py there is an import of MySQLdb. Did you create a script called MySQLdb and try to use the real MySQLdb module from inside your script? If so, try changing its name to something like db.py so there aren't any namespace/import problems.
Note that your user account/password stuff have nothing to do with the error. The error is an attribute error which means something isn't defined that should be. Get that MySQLdb script renamed to something else and your problems might all go away :)
Thanks Greg,
That was it ... I changed the name of my py script to db.py and it worked. I never thought of a namespace conflict.
Jim