欢迎访问宙启技术站
智能推送

Python函数操作MySQL数据库:实现增删改查等基本功能

发布时间:2023-06-15 09:07:30

Python是一种很流行的编程语言,可以用它完成很多有趣的项目。在Web开发中,Python通常被用来做后端开发,因为它可以轻松地操作各种数据库,其中包括MySQL数据库。

MySQL数据库是一种非常流行的数据存储方式,它被广泛用于互联网应用中。在Python中,通过使用MySQL Connector API可以轻松地操作MySQL数据库。MySQL Connector API提供了各种方法,可以实现增删改查等基本功能。

本文将详细介绍如何使用Python函数操作MySQL数据库,实现增删改查等基本功能。

1. 安装MySQL Connector API

首先,需要安装MySQL Connector API。从官方网站下载MySQL Connector API的Python版本,然后打开命令行窗口,输入以下命令来安装MySQL Connector API:

pip install mysql-connector-python

2. 连接MySQL数据库

在开始操作MySQL数据库之前,需要连接到MySQL数据库。连接MySQL数据库的代码如下所示:

import mysql.connector

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

print(mydb)

在上述代码中,使用了MySQL Connector API中的“connect”方法来连接MySQL数据库。在连接MySQL数据库时,在host、user和password参数中分别填入要连接到的MySQL服务的主机名、MySQL用户名和MySQL密码。如果连接成功,将显示如下信息:

<mysql.connector.connection_cext.CMySQLConnection object at 0x04F5B4B0>

3. 创建数据库

连接到MySQL数据库后,接下来需要创建一个数据库。下面是一个创建名为“mydatabase”的数据库的Python代码示例:

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")

在上述代码中,使用了MySQL Connector API中的“execute”方法来执行SQL语句。在这种情况下,执行的SQL语句是创建一个名为“mydatabase”的数据库。如果执行成功,则不会返回任何结果。

4. 创建数据表

创建数据库后,接下来需要创建一个数据表。下面是一个创建名为“customers”的数据表的Python代码示例:

import mysql.connector

mydb = mysql.connector.connect(
	host="localhost",
	user="yourusername",
	password="yourpassword",
	database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")

在上述代码中,使用了MySQL Connector API中的“execute”方法来执行SQL语句。在这种情况下,执行的SQL语句是创建一个名为“customers”的数据表,该表包含三个列:id、name和address。其中,id列是一个自增长主键。如果执行成功,则不会返回任何结果。

5. 插入数据

创建数据表后,接下来需要向数据表中插入数据。下面是一个向“customers”数据表中插入一条记录的Python代码示例:

import mysql.connector

mydb = mysql.connector.connect(
	host="localhost",
	user="yourusername",
	password="yourpassword",
	database="mydatabase"
)

mycursor = mydb.cursor()

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")

mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")

在上述代码中,使用了MySQL Connector API中的“execute”方法来执行SQL语句。在这种情况下,执行的SQL语句是向“customers”数据表中插入一条记录,该记录包含两个字段:name和address。在执行SQL语句之前,使用了字符串格式化来设置SQL语句中的值。如果执行成功,则会返回插入的记录数。

6. 查询数据

插入数据后,接下来可以使用“SELECT”语句从数据表中查询数据。下面是一个查询“customers”数据表中的所有记录的Python代码示例:

import mysql.connector

mydb = mysql.connector.connect(
	host="localhost",
	user="yourusername",
	password="yourpassword",
	database="mydatabase"
)

mycursor = mydb.cursor()

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
	print(x)

在上述代码中,使用了MySQL Connector API中的“execute”方法来执行SQL语句。在这种情况下,执行的SQL语句是选择“customers”数据表的所有记录。执行SQL语句后,可以使用“fetchall”方法获取查询结果,并使用循环迭代结果。

7. 更新数据

除了插入和查询数据外,还可以使用“UPDATE”语句来更新数据。下面是一个将数据表“customers”中所有地址为“Valley 345”记录的地址更新为“Canyon 123”的Python代码示例:

import mysql.connector

mydb = mysql.connector.connect(
	host="localhost",
	user="yourusername",
	password="yourpassword",
	database="mydatabase"
)

mycursor = mydb.cursor()

sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) affected")

在上述代码中,使用了MySQL Connector API中的“execute”方法来执行SQL语句。在这种情况下,执行的SQL语句是将数据表“customers”中所有地址为“Valley 345”的记录的地址更新为“Canyon 123”。如果执行成功,则会返回受影响的记录数。

8. 删除数据

最后,可以使用“DELETE”语句来删除数据。下面是一个删除数据表“customers”中地址为“Mountain 21”的记录的Python代码示例:

import mysql.connector

mydb = mysql.connector.connect(
	host="localhost",
	user="yourusername",
	password="yourpassword",
	database="mydatabase"
)

mycursor = mydb.cursor()

sql = "DELETE FROM customers WHERE address = 'Mountain 21'"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) deleted")

在上述代码中,使用了MySQL Connector API中的“execute”方法来执行SQL语句。在这种情况下,执行的SQL语句是删除数据表“customers”中地址为“Mountain 21”的记录。如果执行成功,则会返回受影响的记录数。

总结

在Python中使用MySQL Connector API操作MySQL数据库非常简单。通过使用MySQL Connector API提供的各种方法,可以轻松地实现增删改查等基本功能。本文介绍了如何连接MySQL数据库、创建数据库和数据表、插入、查询、更新和删除数据。在实际项目中,可能需要使用更复杂的SQL语句和Python代码来实现更高级的功能。