查看数据库里有没有数据

#!/usr/bin/env python
# coding:utf8
# author:Z time:2018/8/28
import pymysql

connect=pymysql.Connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='',
    db='haha',
    charset='utf8'

)
cursor=connect.cursor()
sql="""
select count(*) from hehe
"""
cursor.execute(sql)
a=cursor.fetchall()#((0,),)

if a[0][0]==0:#判断数据库是否为空
    print('没有数据')

计算数据库中数据的数量

((0,),)

输出为0,表示没有数据

终端输出的结果

原文地址:https://www.cnblogs.com/z-x-y/p/9546300.html