import mysql.connector# 连接数据库mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase")# 创建游标mycursor = mydb.cursor()# 执行SQL语句sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"val = ("John", "Highway 21")mycursor.execute(sql, val)# 提交更改mydb.commit()# 输出数据print(mycursor.rowcount, "record inserted.")帮我注释
时间: 2023-07-19 20:45:30 浏览: 95
mysql-connector-java-8.0.22.jar
5星 · 资源好评率100%
好的,以下是对代码的注释:
```python
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost", # 数据库主机地址
user="yourusername", # 数据库用户名
password="yourpassword", # 数据库密码
database="yourdatabase" # 数据库名称
)
# 创建游标
mycursor = mydb.cursor()
# 执行SQL语句
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" # SQL插入语句
val = ("John", "Highway 21") # 插入的数据
mycursor.execute(sql, val) # 执行SQL插入语句
# 提交更改
mydb.commit()
# 输出数据
print(mycursor.rowcount, "record inserted.") # 输出插入的行数
```
阅读全文