Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check with python if a table is empty?

Tags:

python

mysql

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

like image 546
planetp Avatar asked Feb 28 '23 10:02

planetp


1 Answers

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!"
like image 103
jathanism Avatar answered Mar 08 '23 07:03

jathanism