import pymysql import pymysql.cursors # 连接MySQL数据库Y connection = pymysql.connect(host='181.31.85.26', user='joyuser', port=3306, passwd='joypawd', db='JOY_HOME', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) try: # 通过cursor创建游标 cursor = connection.cursor() # 创建sql 语句 sql = "select * from JOY_USER where NICKNAME = 'JOY'" # 执行sql语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() # print(results) for data in results: # 打印结果 print(data) except Exception:print("查询失败") cursor.close() # 关闭游标连接 connection.close() # 关闭数据库连接