4

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

2 Answers 2

6

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!"
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.