亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 与 MySQL 连接:连接器、创建数据库、表、插入 [示例]

为了使用 MySQL 与 Python 的连接,您必须具备一些 SQL 知识

在深入之前,让我们了解一下

什么是 MySQL?

MySQL 是一个开源数据库,也是最好的 RDBMS(关系数据库管理系统)类型之一。 MySQLdb 的联合创始人是 Michael Widenius,MySQL 的名字也来源于 Michael 的女儿。

在本教程中,您将学习

如何在 Windows、Linux/Unix 上安装 MySQL Connector Python

在 Linux/Unix 中安装 MySQL:

从官网下载适用于 Linux/Unix 的 RPM 包:https://www.mysql.com/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 连接器库

以下是如何将 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',)

使用 Python 在 MySQL 中更改表

Alter 命令用于修改 SQL 中的表结构。在这里我们将改变学生 表并将主键添加到 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 

Python

  1. Python Print() 语句:如何通过示例打印
  2. Python String strip() 函数与示例
  3. 带有示例的 Python 字符串计数()
  4. Python String format() 举例说明
  5. Python String find() 方法及示例
  6. 带有示例的 Python Lambda 函数
  7. 带有示例的 Python round() 函数
  8. 带有示例的 Python map() 函数
  9. Python Timeit() 与示例
  10. 集合中的 Python 计数器示例
  11. Python 中的 type() 和 isinstance() 示例
  12. Python 列表理解、追加、排序、长度 [示例]