
统计列表有几条数据

查询列表数据
con = pymysql.connect(host="abchost", port=8888, user="test", password="pwd", database="user_table", charset="utf8")
cursor = con.cursor()
try:
# 当id设置为主键,但是没有设置为自增时,则必须给id字段赋值,否则会报错。
sql = "select * from table;"
cursor.execute(sql)
# 单使用一个fetchone会获取索引为0的记录,若使用多个fetchone时,会按照前一个的索引值继续向下获取记录
data1 = cursor.fetchone()
data2 = cursor.fetchone()
data3 = cursor.fetchone()
except Exception as e:
print("error")
cursor.close()
con.close()
print(data1)
print(data2)
print(data3)
另一种写法
import pymysql
def getcontent():
conn = pymysql.connect(
host="abc", port=8888, user="test", password="pwd", database="table", charset="utf8")
cur = conn.cursor()
sql = "SELECT * FROM table1 "
cur.execute(sql)
data = cur.fetchall()
print(data)
para = []
for i in data:
text = {'name': i[0], 'site': i[1], 'account': i[2]}
print(text)
para.append(text)
if __name__ == '__main__':
getcontent()
浙公网安备 33010602011771号