Windows连接服务器的数据库是一个常见的操作,涉及到多个基础概念和技术要点。以下是对这个问题的详细解答:
import mysql.connector
try:
# 配置连接参数
config = {
'user': 'your_username',
'password': 'your_password',
'host': 'server_address',
'database': 'database_name',
'port': 'port_number'
}
# 建立连接
connection = mysql.connector.connect(**config)
# 创建游标对象
cursor = connection.cursor()
# 执行SQL查询
cursor.execute("SELECT * FROM table_name")
# 获取查询结果
results = cursor.fetchall()
for row in results:
print(row)
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
# 关闭连接
if connection.is_connected():
cursor.close()
connection.close()
通过以上步骤和注意事项,您应该能够在Windows系统上成功连接到服务器的数据库。
领取专属 10元无门槛券
手把手带您无忧上云