安装PyMySQL
PyMySQL是一个Python编写的MySQL驱动程序,让我们可以用Python语言操作MySQL数据库。
首先,使用pip安装PyMySQL。
pip install PyMySQL
- 1
使用PyMySQL
简单使用
如果有JDBC等其他语言的数据库学习经验的话,使用PyMySQL非常简单。下面是一个完整的MySQL增删查(没有改)的例子。
import pymysql
import datetime
host = 'localhost'
username = 'root'
password = '12345678'
db_name = 'test'
create_table_sql = """\
CREATE TABLE fuck(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) UNIQUE ,
nickname VARCHAR(255) NOT NULL ,
birthday DATE
)
"""
insert_table_sql = """\
INSERT INTO fuck(username,nickname,birthday)
VALUES('{username}','{nickname}','{birthday}')
"""
query_table_sql = """\
SELECT id,username,nickname,birthday
FROM fuck
"""
delete_table_sql = """\
DELETE FROM fuck
"""
drop_table_sql = """\
DROP TABLE fuck
"""
connection = pymysql.connect(host=host,
user=username,
password=password,
charset='utf8mb4',
db=db_name)
try:
with connection.cursor() as cursor:
print('--------------新建表--------------')
cursor.execute(create_table_sql)
connection.commit()
print('--------------插入数据--------------')
cursor.execute(
insert_table_sql.format(username='yitian', nickname='易天', birthday=datetime.date.today()))
cursor.execute(
insert_table_sql.format(username='zhang3', nickname='张三', birthday=datetime.date.today()))
cursor.execute(
insert_table_sql.format(username='li4', nickname='李四', birthday=datetime.date.today()))
cursor.execute(
insert_table_sql.format(username='wang5', nickname='王五', birthday=datetime.date.today()))
connection.commit()
print('--------------查询数据--------------')
cursor.execute(query_table_sql)
results = cursor.fetchall()
print(f'id\tname\tnickname\tbirthday')
for row in results:
print(row[0], row[1], row[2], row[3], sep='\t')
print('--------------清除数据--------------')
cursor.execute(delete_table_sql)
connection.commit()
print('--------------删除表--------------')
cursor.execute(drop_table_sql)
connection.commit()
finally:
connection.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
如果需要更详细的资料,请查阅pymysql文档或者其他资料。
防止SQL注入
在上面的例子中直接拼接字符串,这不是好办法,因为可能存在SQL注入攻击,更好的解决办法是使用类库提供的函数来传参。所以上面的代码也需要稍作修改。
首先,将带参数的SQL语句改写。
insert_table_sql = """\
INSERT INTO fuck(username,nickname,birthday)
VALUES(%s,%s,%s)
"""
- 1
- 2
- 3
- 4
然后将相应的执行代码也进行修改,execute函数接受一个元组作为SQL参数。所以代码改写为这样。
print('--------------插入数据--------------')
cursor.execute(insert_table_sql, ('yitian', '易天', datetime.date.today()))
cursor.execute(insert_table_sql, ('zhang3', '张三', datetime.date.today()))
cursor.execute(insert_table_sql, ('li4', '李四', datetime.date.today()))
cursor.execute(insert_table_sql, ('wang5', '王五', datetime.date.today()))
connection.commit()
- 1
- 2
- 3
- 4
- 5
- 6
这样,SQL操作就更安全了。如果需要更详细的文档参考PyMySQL文档吧。不过好像这些SQL数据库的实现还不太一样,PyMySQL的参数占位符使用%s
这样的C格式化符,而Python自带的sqlite3模块的占位符好像是?
。因此在使用其他数据库的时候还是仔细阅读文档吧。