I have the following python code to access a MySQL database:

#!usr/bin/python
# server_version.py - retrieve and display database server version

import MySQLdb

con = MySQLdb.connect  (host = "localhost",
			user = "username",
			passwd = "password",
			db = "test")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row(0)
cursor.close ()
conn.close ()

When I run it, I get the following error:
python mysql_server_version.py
Traceback (most recent call last):
File "mysql_server_version.py", line 4, in <module>
import MySQLdb
File "/home/rbushlow/python/MySQLdb.py", line 2, in <module>
import MySQLdb.cursors
ImportError: No module named cursors

Any Ideas what I need to do here?

Recommended Answers

All 2 Replies

When you first connect to the database you assign the connection object to the variable "con", later when you try using cursor() you use the variable "conn" instead, is this just a typo or is that the actual code you used?

I have tried like this and got output like this:

#!usr/bin/python
# server_version.py - retrieve and display database server version

import MySQLdb

con = MySQLdb.connect (host = "localhost",
db = "test")
cursor = con.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchall()
print row
cursor.close ()
con.close ()

output:
(('5.1.30',),)

I think it will helpful to you.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.