安装PyMySQL

PyMySQL是一个Python编写的MySQL驱动程序,让我们可以用Python语言操作MySQL数据库。

首先,使用pip安装PyMySQL。

pip install PyMySQL

使用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()

如果需要更详细的资料,请查阅pymysql文档或者其他资料。

防止SQL注入

在上面的例子中直接拼接字符串,这不是好办法,因为可能存在SQL注入攻击,更好的解决办法是使用类库提供的函数来传参。所以上面的代码也需要稍作修改。

首先,将带参数的SQL语句改写。

insert_table_sql = """\
INSERT INTO fuck(username,nickname,birthday)
VALUES(%s,%s,%s)
"""

然后将相应的执行代码也进行修改,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()

这样,SQL操作就更安全了。如果需要更详细的文档参考PyMySQL文档吧。不过好像这些SQL数据库的实现还不太一样,PyMySQL的参数占位符使用%s这样的C格式化符,而Python自带的sqlite3模块的占位符好像是?。因此在使用其他数据库的时候还是仔细阅读文档吧。

更多相关文章

  1. Intellij Mybatis连接Mysql数据库
  2. 使用plsql访问远程数据库
  3. hsqldb数据库使用
  4. mysql数据库自增id用法大全_MySQL
  5. 我的MSSQL2000数据库自从我的系统从XPHOME升级到XPP版以后表都打
  6. SQL2000数据库脱机/只读/紧急模式 修复
  7. 十万火急!如何远程复制sql2000数据库
  8. 谁能给我讲讲用C#在windows mobile上开发sql server 2005 mobile
  9. 怎么判断连接SqlServer数据库成功

随机推荐

  1. Android源码大放送(实战开发必备)
  2. Android 中进度计算--百分比显示
  3. Androidの异常总结
  4. android 呼入电话的监听(来电监听)转
  5. android自带Base64加密解密
  6. 全志A64 Android7.1屏蔽使用按键进入安全
  7. android应用中去掉标题栏的方法
  8. Android下如何防止横竖屏切换的时候进度
  9. Linux 命令行更新指定版本 android sdk
  10. 修改应用按键无响应的时间