You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
918 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pymysql
# 数据库连接参数
db_config = {
'host': 'localhost', # 数据库地址默认localhost
'port': 3306, # 数据库端口默认3306
'user': 'root', # 数据库用户名
'password': '21412030117', # 数据库密码
'database': 'word', # 要连接的数据库名
'charset': 'utf8mb4', # 字符编码推荐使用utf8mb4支持更多字符集
}
# 尝试建立连接
try:
connection = pymysql.connect(**db_config)
print("连接MySQL数据库成功")
# 创建游标
cursor = connection.cursor()
# 执行SQL查询
sql_query = "SELECT * FROM words"
cursor.execute(sql_query)
# 获取查询结果
results = cursor.fetchall()
for row in results:
print(row)
# 关闭游标和连接
cursor.close()
connection.close()
except pymysql.MySQLError as e:
print(f"连接数据库时发生错误: {e}")