Using python and MySQLdb, how can I check if there are any records in a mysql table (innodb)?
2 Answers
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.