0

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?

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by rajasekhar1242
1

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?

0

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.....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.