tags:

views:

58

answers:

2

Using python and MySQLdb, how can I check if there are any records in a mysql table (innodb)?

+4  A: 

Just select a single row. If you get nothing back, it's empty! (Example from the MySQLdb site)

import MySQLdb
db = MySQLdb.connect(passwd="moonpie", db="thangs")
results = db.query("""SELECT * from mytable limit 1""")
if not results:
    print "This table is empty!"
jathanism
+2  A: 

Something like

import MySQLdb
db = MySQLdb.connect("host", "user", "password", "dbname")
cursor = db.cursor()
sql = """SELECT count(*) as tot FROM simpletable"""
cursor.execute(sql)
data = cursor.fetchone()
db.close()
print data

will print the number or records in the simpletable table.
You can then test if to see if it is bigger than zero.

Davide Gualano