📜  使用 MySQL-Connector Python连接 MySQL 数据库(1)

📅  最后修改于: 2023-12-03 15:36:32.181000             🧑  作者: Mango

使用 MySQL-Connector Python连接 MySQL 数据库

MySQL是一种流行的关系型数据库管理系统,而Python是一种最受欢迎的编程语言之一。MySQL-Connector Python是Python语言连接MySQL数据库的官方驱动程序。在本篇文章中,我们将学习如何使用MySQL-Connector Python连接MySQL数据库。

安装MySQL-Connector Python

首先,我们需要安装MySQL-Connector Python。您可以使用以下命令在终端中安装:

pip install mysql-connector-python
连接MySQL数据库

在进行任何数据库操作之前,我们需要连接到MySQL数据库。使用MySQL-Connector Python,我们可以通过以下方式连接到MySQL数据库:

import mysql.connector

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

print(mydb)

在这个例子中,我们创建了一个数据库连接对象' mydb '。 我们需要提供MySQL服务器的主机名,用户名,密码以及我们要连接的数据库名称。 如果连接成功,则输出连接对象的字符串表示形式。

创建数据库

接下来,我们将学习如何创建数据库。 它可以通过使用execute()方法执行SQL CREATE DATABASE语句来完成。

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")

在这个例子中,我们创建了游标对象,使用execute()方法执行SQL CREATE DATABASE语句,以创建一个名为'mydatabase'的新数据库。

创建数据表

接下来,我们将学习如何创建数据表。 它可以通过使用execute()方法执行SQL CREATE TABLE语句来完成。

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))")

在这个例子中,我们创建了一个名为“customers”的新数据表。 它有三个列:id,name和address。

插入数据

接下来,我们将学习如何向数据表中插入数据。 它可以通过使用execute()方法执行SQL INSERT INTO语句来完成。

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.")

在这个例子中,我们使用execute()方法执行SQL INSERT INTO语句来将新行插入到数据表“customers”中。

查询数据

接下来,我们将学习如何查询数据。 它可以通过使用execute()方法执行SQL SELECT语句来完成。

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)

在这个例子中,我们使用execute()方法执行SQL SELECT语句来检索所有数据表“customers”的数据,使用fetchall()方法返回结果并打印每一行。

更新数据

接下来,我们将学习如何更新数据。 它可以通过使用execute()方法执行SQL UPDATE语句来完成。

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 = 'Highway 21'"

mycursor.execute(sql)

mydb.commit()

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

在这个例子中,我们使用execute()方法执行SQL UPDATE语句来更新数据表“customers”的行。

删除数据

最后,我们将学习如何删除数据。它可以通过使用execute()方法执行SQL DELETE语句来完成。

import mysql.connector

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

mycursor = mydb.cursor()

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

mycursor.execute(sql)

mydb.commit()

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

在这个例子中,我们使用execute()方法执行SQL DELETE语句从数据表“customers”中删除行。

以上就是使用MySQL-Connector Python连接MySQL数据库的介绍。 您现在可以开始使用MySQL-Connector Python执行各种数据库操作。