MySQL 是最流行的关系型数据库管理系统。
本章节我们为大家介绍使用 mysql-connector 来连接使用 MySQL, mysql-connector 是 MySQL 官方提供的驱动器。
我们可以使用 pip 命令来安装 mysql-connector:
python3 -m pip install mysql-connector
使用以下代码测试 mysql-connector 是否安装成功:
import mysql.connector
执行以上代码,如果没有产生错误,表明安装成功。
import mysql.connector
mydb = mysql.connector.connect(
host="localhost", # 数据库主机地址
user="root", # 数据库用户名
passwd="" # 数据库密码
)
print(mydb)
#创建数据库
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE test2_db")
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
('information_schema',)
('mysql',)
('performance_schema',)
('sys',)
('test1_db',)
('test2_db',)
('test_db',)
#或者我们可以直接连接数据库,如果数据库不存在,会输出错误信息
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db'
)
# 创建数据表
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE test(name VARCHAR(255),url VARCHAR(255))")
#显示数据表
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)
('test',)
#设置主键
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("ALTER TABLE test ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")
#插入一条记录
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
sql = "INSERT INTO test (name,url) VALUES (%s,%s)"
val = ("ECUST","Xu Huan")
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount,'记录插入成功')
1 记录插入成功
#插入多条记录
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
sql = "INSERT INTO test (name,url) VALUES (%s,%s)"
val = [("ECUST","Xu Huan"),
("MIT","Wang Yizhuo"),
("JHU","Nawukere"),
]
mycursor.executemany(sql,val)#使用executemany()插入多条记录
mydb.commit()
print(mycursor.rowcount,'记录插入成功')
3 记录插入成功
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
sql = "INSERT INTO test (name,url) VALUES (%s,%s)"
val = [("PKU","Smith"),
]
mycursor.executemany(sql,val)
mydb.commit()
print('一条记录已插入',mycursor.lastrowid)
一条记录已插入 7
#查询数据可以用SELECT语句
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM test")
myresult = mycursor.fetchall() #fetchall()获取所有记录
for i in myresult:
print(i)
('ECUST', 'Xu Huan', 1)
('ECUST', 'Xu Huan', 2)
('ECUST', 'Xu Huan', 3)
('MIT', 'Wang Yizhuo', 4)
('JHU', 'Nawukere', 5)
('PKU', 'Smith', 6)
('PKU', 'Smith', 7)
#也可以读取指定的字段数据
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SELECT name,id FROM test")
myresult = mycursor.fetchall()
for i in myresult:
print(i)
('ECUST', 1)
('ECUST', 2)
('ECUST', 3)
('MIT', 4)
('JHU', 5)
('PKU', 6)
('PKU', 7)
#如果只想读取一条数据,可以使用fetchone()方法
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM test")
myresult = mycursor.fetchone()
print(myresult)
('ECUST', 'Xu Huan', 1)
#对读取的数据有条件限制,可以使用where语句
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM test WHERE name = 'ECUST'")
myresult = mycursor.fetchall()
for i in myresult:
print(i)
('ECUST', 'Xu Huan', 1)
('ECUST', 'Xu Huan', 2)
('ECUST', 'Xu Huan', 3)
#也可以使用通配符%
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM test WHERE name LIKE '%K%'")
myresult = mycursor.fetchall()
for i in myresult:
print(i)
('PKU', 'Smith', 6)
('PKU', 'Smith', 7)
#为了防止数据库查询发生SQL注入的攻击,可以使用%s占位符转义查询的条件
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
sql = "SELECT * FROM test WHERE name = %s"
na = ('PKU',)
mycursor.execute(sql, na)
myresult = mycursor.fetchall()
for i in myresult:
print(i)
('PKU', 'Smith', 6)
('PKU', 'Smith', 7)
#排序
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM test ORDER BY url")
myresult = mycursor.fetchall()
for i in myresult:
print(i)
('JHU', 'Nawukere', 5)
('PKU', 'Smith', 6)
('PKU', 'Smith', 7)
('MIT', 'Wang Yizhuo', 4)
('ECUST', 'Xu Huan', 1)
('ECUST', 'Xu Huan', 2)
('ECUST', 'Xu Huan', 3)
#对数据基于某属性降序排序
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM test ORDER BY name DESC")
myresult = mycursor.fetchall()
for i in myresult:
print(i)
('PKU', 'Smith', 6)
('PKU', 'Smith', 7)
('MIT', 'Wang Yizhuo', 4)
('JHU', 'Nawukere', 5)
('ECUST', 'Xu Huan', 1)
('ECUST', 'Xu Huan', 2)
('ECUST', 'Xu Huan', 3)
#读取指定数量条数据
#限制读取数据起始数据
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM test LIMIT 4 OFFSET 2")
#用LIMIT语句指定查询数据量
#用OFFSET语句限制起始位置
myresult = mycursor.fetchall()
for i in myresult:
print(i)
('ECUST', 'Xu Huan', 3)
('MIT', 'Wang Yizhuo', 4)
('JHU', 'Nawukere', 5)
('PKU', 'Smith', 6)
#使用DELETE FROM语句删除记录
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
mycursor.execute("DELETE FROM test WHERE name LIKE '%K%'")
mydb.commit()
print(mycursor.rowcount, " 条记录删除")
2 条记录删除
#为了防止数据库查询发生 SQL 注入的攻击,我们可以使用 %s 占位符来转义删除语句的条件
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
sql = "DELETE FROM test WHERE name = %s"
na = ("ECUST",)
mycursor.execute(sql,na)
mydb.commit()
print(mycursor.rowcount, " 条记录删除")
3 条记录删除
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
sql = "UPDATE test SET name = 'princeton' WHERE name = 'JHU'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount,'条记录被修改')
2 条记录被修改
#为了防止数据库查询发生 SQL 注入的攻击,我们可以使用 %s 占位符来转义更新语句的条件
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
sql = "UPDATE test SET name = %s WHERE name = %s"
na = ('JHU','princeton')
mycursor.execute(sql,na)
mydb.commit()
print(mycursor.rowcount,'条记录被修改')
2 条记录被修改
#使用DROP TABLE语句删除表,使用IF EXISTS关键字判断表是否尊在
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = 'root',
passwd = '',
database = 'test_db',
)
mycursor = mydb.cursor()
sql = 'DROP TABLE IF EXISTS test'
mycursor.execute(sql)