Python 与 MySQL 连接:数据库和表[示例]
为了使用 Python 的 MySQL 连接性,您必须对 SQL 有一些了解。
在深入研究之前,让我们先了解一下
什么是 MySQL?
MySQL 是一个开源数据库,也是 RDBMS(关系数据库管理系统)的最佳类型之一。MySQLdb 的联合创始人是 Michael Widenius,MySQL 的名字也来源于 Michael 的女儿。
如何在 Windows、Linux/Unix 上安装 MySQL Connector Python
在 Linux/Unix 中安装 MySQL
从官方网站下载 Linux/Unix 的 RPM 包: https://mysqlserver.cn/downloads/
在终端中使用以下命令
rpm -i <Package_name>
Example rpm -i MySQL-5.0.9.0.i386.rpm
在 Linux 中检查
mysql --version
在 Windows 中安装 MySQL
从官方网站下载 MySQL 数据库 exe 文件,然后像往常一样在 Windows 中进行软件安装。请参考本教程,获取分步指南。
如何为 Python 安装 MySQL Connector 库
以下是如何将 MySQL 与 Python 连接
对于 Python 2.7 或更低版本,请使用 pip 进行安装,命令如下:
pip install mysql-connector
对于 Python 3 或更高版本,请使用 pip3 进行安装,命令如下:
pip3 install mysql-connector
用 Python 测试 MySQL 数据库连接
在此测试 Python 的 MySQL 数据库连接性,我们将使用预装的 MySQL 连接器,并将凭据传递到 connect() 函数中,如主机、用户名和密码,如下面的 Python MySQL 连接器示例所示。
使用 Python 访问 MySQL 的语法
import mysql.connector db_connection = mysql.connector.connect( host="hostname", user="username", passwd="password" )
示例:
import mysql.connector db_connection = mysql.connector.connect( host="localhost", user="root", passwd="root" ) print(db_connection)
输出:
<mysql.connector.connection.MySQLConnection object at 0x000002338A4C6B00>
此处输出显示连接已成功创建。
使用 Python 在 MySQL 中创建数据库
在 SQL 中创建新数据库的语法是
CREATE DATABASE "database_name"
现在我们通过 Python 中的数据库编程来创建数据库
import mysql.connector db_connection = mysql.connector.connect( host= "localhost", user= "root", passwd= "root" ) # creating database_cursor to perform SQL operation db_cursor = db_connection.cursor() # executing cursor with execute method and pass SQL query db_cursor.execute("CREATE DATABASE my_first_db") # get list of all databases db_cursor.execute("SHOW DATABASES") #print all databases for db in db_cursor: print(db)
输出:
上图显示已创建 my_first_db 数据库
使用 Python 在 MySQL 中创建表
让我们创建一个简单的“student”表,其中包含两列,如下面的 MySQL 连接器 Python 示例所示。
SQL 语法:
CREATE TABLE student (id INT, name VARCHAR(255))
示例
import mysql.connector db_connection = mysql.connector.connect( host="localhost", user="root", passwd="root", database="my_first_db" ) db_cursor = db_connection.cursor() #Here creating database table as student' db_cursor.execute("CREATE TABLE student (id INT, name VARCHAR(255))") #Get database table' db_cursor.execute("SHOW TABLES") for table in db_cursor: print(table)
输出:
('student',)
使用主键创建表
让我们创建一个包含三个不同列的 Employee 表。我们将为 id 列添加主键,并带有 AUTO_INCREMENT 约束,如下面的带有数据库连接性的 Python 项目所示。
SQL 语法:
CREATE TABLE employee(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), salary INT(6))
示例:
import mysql.connector db_connection = mysql.connector.connect( host="localhost", user="root", passwd="root", database="my_first_db" ) db_cursor = db_connection.cursor() #Here creating database table as employee with primary key db_cursor.execute("CREATE TABLE employee(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), salary INT(6))") #Get database table db_cursor.execute("SHOW TABLES") for table in db_cursor: print(table)
输出:
('employee',) ('student',)
在 MySQL 中使用 Python 进行 ALTER table 操作
Alter 命令用于在 SQL 中修改表结构。此处我们将修改 Student 表,并为 id 字段添加一个主键,如下面的 Python MySQL 连接器项目所示。
SQL 语法:
ALTER TABLE student MODIFY id INT PRIMARY KEY
示例:
import mysql.connector db_connection = mysql.connector.connect( host="localhost", user="root", passwd="root", database="my_first_db" ) db_cursor = db_connection.cursor() #Here we modify existing column id db_cursor.execute("ALTER TABLE student MODIFY id INT PRIMARY KEY")
输出:
您可以在下方看到 id 列已修改。
使用 Python 在 MySQL 中执行插入操作
让我们对我们已创建的 MySQL 数据库表执行插入操作。我们将向 STUDENT 表和 EMPLOYEE 表插入数据。
SQL 语法:
INSERT INTO student (id, name) VALUES (01, "John") INSERT INTO employee (id, name, salary) VALUES(01, "John", 10000)
示例:
import mysql.connector db_connection = mysql.connector.connect( host="localhost", user="root", passwd="root", database="my_first_db" ) db_cursor = db_connection.cursor() student_sql_query = "INSERT INTO student(id,name) VALUES(01, 'John')" employee_sql_query = " INSERT INTO employee (id, name, salary) VALUES (01, 'John', 10000)" #Execute cursor and pass query as well as student data db_cursor.execute(student_sql_query) #Execute cursor and pass query of employee and data of employee db_cursor.execute(employee_sql_query) db_connection.commit() print(db_cursor.rowcount, "Record Inserted")
输出:
2 Record Inserted