先安装pymysql import pymysql # 连接到数据库 connection = pymysql.connect(host='localhost',port=3306, user='root', password='123', db='slb', charset='utf8', ) try: # with connection.cursor() as cursor: # # 插入新的数据 # sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" # cursor.execute(sql, ('webmaster@python.org', 'very-secret')) # # # 默认情况下,连接不是自动提交。所以你必须自行保存好 # connection.commit() with connection.cursor() as cursor: # 读取单个记录 sql = "SELECT * FROM slb.slb_machine_maintain" cursor.execute(sql) result = cursor.fetchone() print(result) finally: connection.close()