Mysql 简明教程

MySQL - Python Syntax

MySQL-Python 连接器专门指 Python 中的一个库,它可以实现 Python 程序与 MySQL 数据库之间的通信。它充当桥梁,允许 Python 程序与存储在 MySQL 数据库中的数据进行交互和操作。从本质上看,MySQL-Python 连接器简化了连接、查询和管理数据库的过程,使开发者能够无缝地将他们的 Python 应用程序与 MySQL 数据库集成在一起。

The MySQL-Python connector specifically refers to a library in Python that enables communication between a Python program and a MySQL database. It acts as a bridge, allowing Python programs to interact with and manipulate data stored in a MySQL database. Essentially, the MySQL-Python connector simplifies the process of connecting, querying, and managing databases, enabling developers to seamlessly integrate their Python applications with MySQL databases.

Installing "python-mysql" connector

要将 MySQL 与 Python 结合使用,您通常需要安装一个 MySQL 连接器或库。以下是安装它的常规步骤 −

To use MySQL with Python, you typically need to install a MySQL connector or library. Here are the general steps to install it −

Step 1: Install MySQL Server

Step 1: Install MySQL Server

确保您在计算机上安装了 MySQL 服务器或者可以访问远程 MySQL 服务器。

Make sure you have MySQL Server installed on your machine or have access to a remote MySQL server.

Step 2: Install MySQL Connector for Python

Step 2: Install MySQL Connector for Python

打开命令提示符或终端,并使用以下命令使用 pip(Python 的软件包安装器)安装 MySQL Connector for Python:

Open a command prompt or terminal and use the following command to install the MySQL Connector for Python using pip, which is the package installer for Python:

pip install mysql-connector-python

如果您使用的是 Python 3,您可能需要使用 'pip3' 而不是 'pip'。

If you are using Python 3, you might need to use 'pip3' instead of 'pip'.

  • Step 3: Verify Installation*

安装完成后,您可以通过打开 Python 交互式 shell 并尝试导入连接器来验证该库是否安装完毕:

After the installation is complete, you can verify that the library is installed by opening a Python interactive shell and trying to import the connector:

import mysql.connector

Python Functions to Access MySQL

在 Python 中使用 MySQL 时,“mysql-connector-python”库提供了各种函数来与 MySQL 数据库进行交互。以下是常用的几个重要函数 −

When working with MySQL in Python, the 'mysql-connector-python' library provides various functions to interact with a MySQL database. Here are some important functions commonly used −

Basic Example

若要使用 Python 连接和通信 MySQL 数据库,请按以下步骤操作:

To connect and communicate with a MySQL database using Python, you can follow these steps −

  1. Use 'pip install mysql-connector-python' to install the MySQL Connector for Python.

  2. Import the MySQL Connector module in your Python script: "import mysql.connector".

  3. Create a connection using "mysql.connector.connect()" with your database details.

  4. Create a cursor using "connection.cursor()".

  5. Use the cursor’s "execute()" method to run SQL queries.

  6. If applicable, use "fetchone()" or "fetchall()" to retrieve query results.

  7. If you modify data, commit changes using "connection.commit()".

  8. Close the cursor and connection with "cursor.close()" and "connection.close()".

以下示例展示 Python 程序的一般语法,如何调用任意 MySQL 查询:

The following example shows a generic syntax of a Python program to call any MySQL query −

import mysql.connector
# Establish connection
connection = mysql.connector.connect(host='localhost', user='user', password='pass', database='db')
# Create cursor
cursor = connection.cursor()
# Execute query
cursor.execute("SELECT * FROM table")
# Fetch and print results
rows = cursor.fetchall()
print(rows)
# Close cursor and connection
cursor.close()
connection.close()