用phython 写一个连接数据库 并查询数据库的代码
以下是Python连接数据库并查询数据库的示例代码:
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="mydatabase"
)
# 查询数据
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
# 输出查询结果
for x in mycursor:
print(x)
上述示例代码通过mysql.connector模块连接本地MySQL数据库,并查询customers表中的所有数据。查询结果使用for循环遍历并输出。根据实际情况,可以修改代码中的数据库连接信息和查询语句。
原文地址: https://www.cveoy.top/t/topic/bVQT 著作权归作者所有。请勿转载和采集!