Python - MySQL 连接:数据库和表 [示例]
为了使用 MySQL 与 Python,你必须了解 SQL
在深入探讨之前,让我们先了解一下
什么是 MySQL?
MySQL 是一个开源数据库,也是最好的关系数据库管理系统 (RDBMS) 之一。联合创始人 MySQLdb 是 Michael Widenius 的,还有 MySQL 名字源于迈克尔的女儿。
如何安装 MySQL 接头类型 Python on Windows、Linux/Unix
安装 MySQL 在Linux / Unix中:
从官方网站下载适用于 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
安装 MySQL in Windows
下载 MySQL 数据库 exe 来自 官方网站 并像往常一样安装软件 Windows。请参阅此 教程,获取分步指南
如何安装 MySQL 连接器库 Python
这是如何连接 MySQL - Python:
对于 Python 2.7 或更低版本使用 pip 安装:
pip install mysql-connector
对于 Python 3 或更高版本使用 pip3 安装如下:
pip3 install mysql-connector
测试 MySQL 数据库连接 Python
去测试 MySQL 数据库连接 Python 在这里,我们将使用预安装的 MySQL 连接器并将凭据传递到 connect() 如下所示的主机、用户名和密码等功能 Python MySQL 连接器示例。
访问语法 MySQL - Python:
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>
这里输出显示连接创建成功。
在中创建数据库 MySQL 使用 Python
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)
输出:
上图显示了 我的第一个数据库 数据库已创建
在中创建表 MySQL - Python
让我们创建一个简单的表“学生”,它有两列,如下所示 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',)
创建具有主键的表
让我们创建一个 员工 表有三个不同的列。我们将在 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',)
ALTER 表 MySQL - Python
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 列已被修改。
插页 Opera与 MySQL in Python:
让我们执行插入操作 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 初学者教程:学习编程基础知识 [PDF]


