您的位置:

简单易用的Python连接MySQL数据库方法

一、安装MySQL驱动

要使用Python连接MySQL数据库,需要先安装相应的MySQL驱动。MySQL官方提供了多种语言的驱动程序,其中Python语言的驱动程序为mysql-connector-python。可以使用pip直接安装:

pip install mysql-connector-python

安装完成后,就可以在Python代码中使用该驱动来操作MySQL数据库了。

二、连接MySQL数据库

在Python程序中,需要使用驱动提供的connect()函数来连接MySQL数据库,并指定相应的主机、用户名和密码等参数:

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword"
)

上面的代码中,host参数指定MySQL服务器的IP地址或域名;user参数指定连接MySQL数据库的用户名;passwd参数指定连接MySQL数据库的密码。

三、创建数据库和数据表

连接成功后,需要创建一个数据库并在其中创建数据表。可以使用MySQL的命令行工具或者MySQL Workbench等工具来创建。

使用Python程序来创建一个名为“testdb”的数据库和一个名为“customers”的数据表:

mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE testdb")
mycursor.execute("USE testdb")
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")

四、插入数据

使用Python程序来插入数据到“customers”数据表中:

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")

上面的代码中,使用了SQL语句的INSERT INTO语句来插入数据,%s用来占位。然后使用Python的execute()函数来执行插入语句,其中sql是INSERT INTO语句,val是要插入的数据,是一个元组形式的参数。执行插入语句后,还需要使用commit()函数提交更改才能生效。

五、查询数据

使用Python程序来查询“customers”数据表中的数据:

mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
    print(x)

上面的代码中,使用了SQL语句的SELECT语句来查询数据。使用Python的execute()函数来执行查询语句,然后使用fetchall()函数获取所有结果,返回一个元组形式的列表。然后遍历列表输出结果。

六、更新数据

使用Python程序来更新“customers”数据表中的数据:

sql = "UPDATE customers SET address = 'Canyon 123' WHERE name = 'John'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")

上面的代码中,使用了SQL语句的UPDATE语句来更新数据。使用Python的execute()函数来执行更新语句,然后使用commit()函数提交更改才能生效。执行更新语句后,使用rowcount属性来获取更新记录的数量。

七、删除数据

使用Python程序来删除“customers”数据表中的数据:

sql = "DELETE FROM customers WHERE name = 'John'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")

上面的代码中,使用了SQL语句的DELETE语句来删除数据。使用Python的execute()函数来执行删除语句,然后使用commit()函数提交更改才能生效。执行删除语句后,使用rowcount属性来获取删除记录的数量。

八、完整代码示例

下面是将上面的所有代码整合在一起的完整示例代码:

import mysql.connector

# 连接MySQL数据库
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword"
)

# 创建数据库和数据表
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE testdb")
mycursor.execute("USE testdb")
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")

# 插入数据
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")

# 查询数据
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
    print(x)

# 更新数据
sql = "UPDATE customers SET address = 'Canyon 123' WHERE name = 'John'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")

# 删除数据
sql = "DELETE FROM customers WHERE name = 'John'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")