为了更好的用Python使用MySQL,您必须具备一些SQL知识
在深入探讨之前,让我们了解一下
什么是MySQL?
MySQL是一个开源数据库,也是RDBMS(关系数据库管理系统)的最佳类型之一。MySQLdb的共同创始人是Michael Widenius,而MySQL的名字也源于Michael的女儿。
在Windows中安装MySQL
从官方站点下载MySQL数据库exe,然后像往常一样在Windows中正常安装软件。
安装适用于Python的MySQL连接器库
pip install mysql-connector
使用Python测试MySQL数据库连接
为了在这里测试数据库连接,我们使用预安装的MySQL连接器,并将凭据传递给connect()函数,例如主机,用户名和密码。
使用Python访问MySQL的语法:
import mysql.connector db_connection = mysql.connector.connect( host="hostname", user="username", passwd="password" )
Example,
import mysql.connector db_connection = mysql.connector.connect( host="localhost", user="root", passwd="root" ) print(db_connection)
Output:
<mysql.connector.connection.MySQLConnection object at 0x000001F0A22C7BB0>
此输出显示成功创建的连接。
使用Python在MySQL中创建数据库
在SQL中创建新数据库的语法是
CREATE DATABASE "database_name"
现在我们在MySQL中使用Python创建数据库
import mysql.connector db_connection = mysql.connector.connect( host= "localhost", user= "root", passwd= "" ) db_cursor = db_connection.cursor() db_cursor.execute("CREATE DATABASE my_first_db") db_cursor.execute("SHOW DATABASES")
上图显示了my_first_db数据库的创建
使用Python在MySQL中创建表
让我们创建一个简单的表“ student”,它有两列。
SQL语法:
CREATE TABLE student (id INT, name VARCHAR(255))
Example:
import mysql.connector db_connection = mysql.connector.connect( host="localhost", user="root", passwd="", database="my_first_db" ) db_cursor = db_connection.cursor() db_cursor.execute("CREATE TABLE student (id INT, name VARCHAR(255))") db_cursor.execute("SHOW TABLES") for table in db_cursor: print(table)
Output:
('student',)
用主键创建表
让我们创建一个包含三个不同列的Employee表。我们将在id列中添加具有AUTO_INCREMENT约束的主键
SQL语法
CREATE TABLE employee(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), salary INT(6))
Example,
import mysql.connector db_connection = mysql.connector.connect( host="localhost", user="root", passwd="root", database="my_first_db" ) db_cursor = db_connection.cursor() db_cursor.execute("CREATE TABLE employee(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), salary INT(6))") db_cursor.execute("SHOW TABLES") for table in db_cursor: print(table)
Output:
('employee',) ('student',)
使用Python的MySQL中的ALTER表
Alter命令用于在SQL中修改表结构。在这里,我们将更改Student表并将主键添加到id字段。
SQL语法
ALTER TABLE student MODIFY id INT PRIMARY KEY
Example,
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")
Output:
在下面,您可以看到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)
Example,
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")
Output:
1 Record Inserted