Introduction • The Python programming language has powerful features for database programming • Python supports various databases like MySQL, Oracle, Sybase, PostgreSQL, etc • Python also supports Data Definition Language (DDL), Data Manipulation Language (DML) and Data Query Statements • For database programming, the Python DB API is a widely used module that provides a database application programming interfacePython 编程语言具有强大的数据库编程功能 - Python 支持 MySQL、Oracle、Sybase、PostgreSQL 等各种数据库 - Python 还支持数据定义语言 (DDL)、数据操作语言 (DML) 和数据查询语句 - 对于数据库编程,Python DB API 是一个广泛使用的模块,它提供了一个数据库应用程序编程接口
Interface • Interface is the way for an application to interact with certain system/application接口是应用程序与某些系统/应用程序交互的方式API – Application Programming Interface • A set of subroutine definitions, communication protocols, and tools for building software • In general, it is a set of clearly defined methods of communication among various components. • The API is very much similar to the waiter. API is the messenger that takes your order (waiter) and tells the system (kitchen) what to do (to prepare food) and in return gives back the response you asked for (waiter returns with the ordered food). Application Programming Interface(应用程序接口)- 用于构建软件的一套子程序定义、通信协议和工具- 一般来说,它是一套明确定义的不同组件之间的通信方法。 api是信使,接受命令,通知系统怎么做,给出所要求的回应
Benefits of Python for Database Programming There are many good reasons to use Python for programming database applications: • Programming in Python is arguably more efficient and faster compared to other languages • Python is famous for its portability • It is platform independent • Python supports SQL cursors • In many programming languages, the application developer needs to take care of the open and closed connections of the database, to avoid further exceptions and errors. In Python, these connections are taken care of • Python supports relational database systems • Python database APIs are compatible with various databases, so it is very easy to migrate and port database application interfaces使用 Python 进行数据库编程的好处 与其他语言相比,用 Python 编程可以说更高效、更快速 - Python 以其可移植性而闻名 - Python 与平台无关 - Python 支持 SQL 游标 - 在许多编程语言中,应用程序开发人员需要处理数据库的打开和关闭连接,以避免进一步的异常和错误。 Python 支持关系数据库系统 - Python 数据库 API 与各种数据库兼容,因此迁移和移植数据库应用程序接口非常容易。
Python Integration with MySQL Total 5 modules available in python to communicate with a MySQL and provides MySQL database support to our applications and they are: • MySQL Connector Python (We will use this throughout this course!) • PyMySQL • mysqlclient • MySQLDB • OurSQL Python 与 MySQL 的集成 Python 中共有 5 个模块可用于与 MySQL 通信• MySQL Connector Python (重点) • PyMySQL • mysqlclient • MySQLDB • OurSQL
MySQL Connector Python • What is MySQL Connector Python? — MySQL Connector Python is module or library available in python to communicate with a MySQL • MySQL Connector Python is written in pure Python, and it is self-sufficient to execute database queries through python • It is an official Oracle-supported driver to work with MySQL and python • It is Python 3 compatible, actively maintained /MySQL Connector Python是Python中可用来与MySQL通信的模块或库 - MySQL Connector Python由纯Python语言编写,可通过Python自给自足地执行数据库查询 - 它是Oracle官方支持的驱动程序,可与MySQL和Python协同工作 - 它与Python 3兼容
Prerequisites and Platforms • Need root or administrator privileges to perform the installation process • Python must installed on your machine • Platform(s): Windows, Linux, MacOS • Python version(s): Python 2 and 3 and above • MySQL Version(s): Greater than 4.1 前提条件和平台 - 需要 root 或管理员权限才能执行安装过程 - 机器上必须安装 Python - 平台: Windows、Linux、MacOS - Python 版本: Python 2 和 3 及以上 - MySQL 版本: 大于 4.1
Ways to Install MySQL Connector Python There are multiple ways to install Oracle’s MySQL Connector Python on your machine. Following are the few ways • Install MySQL Connector Python using the pip command• Install MySQL connector Python via source code (via ZIP or TAR file) • Install MySQL connector Python via Anaconda 安装 Oracle 的 MySQL Connector Python 有多种方法:使用 pip 命令安装 MySQL 连接器 Python - pip install mysql-connector-python - 通过源代码安装 MySQL 连接器 Python(通过 ZIP 或 TAR 文件) - 通过 Anaconda 安装 MySQL 连接器 Python
Python MySQL Database Connection Goals – • How to connect MySQL Server and create a table in MySQL from Python • Different MySQL Connection arguments we can use to connect to MySQL • How to change the MySQL connection timeout when connecting through Python 可以使用不同的 MySQL 连接参数来连接 MySQL - 通过 Python 连接时如何更改 MySQL 连接超时
Arguments Required to Connect MySQL from Python You need to know the following detail of the MySQL server to perform the connection from Python • Username – i.e., the username that you use to work with MySQL Server. The default username for the MySQL database is a root • Password – Password is given by the user at the time of installing the MySQL database. If you are using root then you won’t need the password • Host Name – is the server name or IP address on which MySQL is running. if you are running on localhost, then you can use localhost, or it’s IP, i.e., 127.0.0.1 • Database Name – Database name to which you want to connect 从 Python 连接 MySQL 所需的参数 要从 Python 执行连接,需要知道 MySQL 服务器的以下详细信息 - 用户名 - 即使用 MySQL 服务器工作时使用的用户名。MySQL 数据库的默认用户名是 root - 密码 - 用户在安装 MySQL 数据库时给出的密码。如果使用的是 root,则不需要密码 - Host Name(主机名)- 是运行 MySQL 的服务器名称或 IP 地址
Steps to Connect MySQL Database in Python using MySQL Connector Python • Install MySQL Connector Python using pip • Use the mysql.connector.connect() method of MySQL Connector Python with required parameters to connect MySQL • Use the connection object returned by a connect() method to create a cursor object to perform Database Operations • The cursor.execute() to execute SQL queries from Python • Close the Cursor object using a cursor.close() and MySQL database connection using connection.close() after your work completes • Catch Exception if any that may occur during this process在Python中连接MySQL数据库的步骤--使用pip安装MySQL Connector Python--使用MySQL Connector Python的mysql.connector.connect()方法和所需参数连接MySQL--使用connect()方法返回的连接对象创建游标对象以执行数据库操作--使用cursor.execute()从Python执行SQL查询--工作完成后使用cursor.close()关闭游标对象,并使用connection.close()关闭MySQL数据库连接--捕捉此过程中可能出现的任何异常
Steps to Connect MySQL Database in Python using MySQL Connector Python Follow the steps: • Step 1: Start the Python • Step 2: Import package • Step 3: Open connection or connect to database • Step 4: Create a cursor • Step 5: Execute query • Step 6: Extract data from the result set • Step 7: Close the connection or clean up the environment 第 1 步:启动 Python - 第 2 步:导入软件包 - 第 3 步:打开连接或连接到数据库 - 第 4 步:创建游标 - 第 5 步:执行查询 - 第 6 步:从结果集中提取数据 - 第 7 步:关闭连接或清理环境
• What is Database Connection Object? — A Database connection object controls the connection to the database. It represents a unique session with a database connected from within a script or program — One can check the connection by writing the following code
• What is cursor? — A database cursor is a special control structure that facilitates the row by processing of records in the result set • What is result set? — Result set refers to the logical set of records that are fetched from the database by executing an SQL query. It is the set of records retrieved as per the query 什么是游标? - 数据库游标是一种特殊的控制结构,可以方便地逐行处理结果集中的记录 - 什么是结果集? - 结果集是指通过执行 SQL 查询从数据库中获取的逻辑记录集。它是根据查询检索到的记录集
• Extract data from the result set — After retrieving the records from the DB using SQL Select Query. You need to extract records from the result set使用 SQL 选择查询从数据库检索记录后,需要从结果集中提取记录
• You can extract the result set using any of the following fetch functions/cursor methods提取函数/游标方法提取结果集
.fetchone() - 取查询结果集的下一行,返回单个序列,如果没有更多数据,则返回 None
.fetchmany(n) - Fetch many(n) 方法将以包含记录的元组形式从结果集中只返回 n 条记录
.fetchall() - 全部获取方法将以包含记录的元组形式返回结果集中的所有记录
Parameterised Queries You can run the queries with parameters 参数化查询 运行带参数的查询• Example
These kind of queries are called as parameterised queries • To form a parameterised queries there are two methods 形成参数化查询的两种方法
Forming Query Strings