• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Pyhton中防止SQL注入的方法

Pyhton中防止SQL注入的方法

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了pyhton,pyhton下载,pyhton语言,pyhton macd,pyhton官网等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

c=db.cursor()
max_price=5
c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""", (max_price,))
</div>
注意,上面的SQL字符串与后面的tuple之间的分隔符是逗号,平时拼写SQL用的是%。

如果按照以下写法,是容易产生SQL注入的:
c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""" % (max_price,))
</div>

这个和PHP里的PDO是类似的,原理同MySQL Prepared Statements。

Python

Using the Python DB API, don't do this:

# Do NOT do it this way.
cmd = "update people set name='%s' where id='%s'" % (name, id) curs.execute(cmd)
</div>
Instead, do this:
cmd = "update people set name=%s where id=%s" curs.execute(cmd, (name, id))
</div>
Note that the placeholder syntax depends on the database you are using.

The values for the most common databases are:
>>> import MySQLdb; print MySQLdb.paramstyle format >>> import psycopg2; print psycopg2.paramstyle pyformat >>> import sqlite3; print sqlite3.paramstyle qmark
</div>
So if you are using MySQL or PostgreSQL, use %s (even for numbers and other non-string values!) and if you are using SQLite use ?

</div>

您可能想查找下面的文章:

  • 解析Mac OS下部署Pyhton的Django框架项目的过程
  • 解析Mac OS下部署Pyhton的Django框架项目的过程
  • Pyhton中防止SQL注入的方法

相关文章

  • Python文件去除注释的方法
  • python使用rabbitmq实现网络爬虫示例
  • Python获取Windows或Linux主机名称通用函数分享
  • Python对象转JSON字符串的方法
  • Python的函数的一些高阶特性
  • Python单链表的简单实现方法
  • 浅析python 中__name__ = '__main__' 的作用
  • python开启多个子进程并行运行的方法
  • Python实现批量将word转html并将html内容发布至网站的方法
  • Python Web服务器Tornado使用小结

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python实现将DOC文档转换为PDF的方法
    • Python全局变量操作详解
    • Django日志模块logging的配置详解
    • python计算圆周率pi的方法
    • python创建和使用字典实例详解
    • Python中的anydbm模版和shelve模版使用指南
    • python中遍历文件的3个方法
    • Python中.py文件打包成exe可执行文件详解
    • python制作最美应用的爬虫
    • python中元类用法实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有