为了使用Python使用MySQL,您必须具备一些SQL知识
在深入探讨之前,让我们了解一下
MySQL是一个开源数据库,也是RDBMS(关系数据库管理系统)的最佳类型之一。 MySQLdb的共同创始人是Michael Widenius,而MySQL的名字也源于Michael的女儿。
在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
To check in Linux
mysql --version
在Windows中安装MySQL
从官方站点下载MySQL数据库exe,然后像往常一样在Windows中正常安装软件。
对于Python 2.7或更低版本,请使用pip进行安装:
pip install mysql-connector
对于Python 3或更高版本,请使用pip3进行安装:
pip3 install mysql-connector
为了在这里测试数据库连接,我们使用预安装的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 0x000002338A4C6B00>
此输出显示成功创建的连接。
在SQL中创建新数据库的语法是
CREATE DATABASE "database_name"
现在我们在MySQL中使用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)
Output:
上图显示了my_first_db数据库的创建
让我们创建一个简单的表“ student”,它有两列。
SQL语法:
CREATE TABLE student (id INT, name VARCHAR(255))
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 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)
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()
#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)
Output:
('employee',) ('student',)
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列已修改。
让我们在已经创建的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:
2 Record Inserted