import pymysql # 链接数据库 con = pymysql.connect(host='localhost', user='root', password='123456', database='mytest', charset='utf8') # 创建游标 cur = con.cursor() # 生成数据库 sql = 'select * from mytable' # 获取结果 cur.execute(sql) # 获取所有记录 fetchall 获取所有记录 fetchmany 获取多条记录,需传参 fetchone 获取一条记录 dall = cur.fetchall() # 输出查询结果 print(dall) # 关闭游标 cur.close() # 关闭数据库连接,释放内存 cur.close() # 打印结果 ((1, 'zhangs', 19),)