Python操作sqlite3快速、安全插入数据(防注入)的实例

 更新时间:2014年04月26日 09:24:07   作者:  
这篇文章主要介绍了Python操作sqlite3快速、安全插入数据(防注入)的实例,通过在一个表格中进行操作来论述如何使用Python快速安全地操作sqlite3,需要的朋友可以参考下


table通过使用下面语句创建:

复制代码 代码如下:
create table userinfo(name text, email text)

更快地插入数据

在此用time.clock()来计时,看看以下三种方法的速度。

复制代码 代码如下:

import sqlite3
import time

def create_tables(dbname): 
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''create table userinfo(name text, email text)''')
    conn.commit()
    cursor.close()
    conn.close()
def drop_tables(dbname):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''drop table userinfo''')
    conn.commit()
    cursor.close()
    conn.close()

def insert1():
    users = [('qq','qq@example.com'),
            ('ww','ww@example.com'),
            ('ee','ee@example.com'),
            ('rr','rr@example.com'),
            ('tt','tt@example.com'),
            ('yy','yy@example.com'),
            ('uu','uu@example.com')
            ]
    start = time.clock()
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    for user in users:
        cursor.execute("insert into userinfo(name, email) values(?, ?)", user)
        conn.commit()
    cursor.close()
    conn.close()
    end = time.clock()
    print start, end, end-start

def insert2():
    users = [('qq','qq@example.com'),
            ('ww','ww@example.com'),
            ('ee','ee@example.com'),
            ('rr','rr@example.com'),
            ('tt','tt@example.com'),
            ('yy','yy@example.com'),
            ('uu','uu@example.com')
            ]
    start = time.clock()
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    for user in users:
        cursor.execute("insert into userinfo(name, email) values(?, ?)", user)
    conn.commit()
    cursor.close()
    conn.close()
    end = time.clock()
    print start, end, end-start

def insert3():
    users = [('qq','qq@example.com'),
            ('ww','ww@example.com'),
            ('ee','ee@example.com'),
            ('rr','rr@example.com'),
            ('tt','tt@example.com'),
            ('yy','yy@example.com'),
            ('uu','uu@example.com')
            ]
    start = time.clock()
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.executemany("insert into userinfo(name, email) values(?, ?)", users)
    conn.commit()
    cursor.close()
    conn.close()
    end = time.clock()
    print start, end, end-start

if __name__ == '__main__':
    dbname = 'test.db'
    create_tables(dbname)
    insert1()
    drop_tables(dbname)
    create_tables(dbname)
    insert2()
    drop_tables(dbname)
    create_tables(dbname)
    insert3()
    drop_tables(dbname)

某次运行结果:

复制代码 代码如下:

4.05223164501e-07 0.531585119557 0.531584714334
0.755963264089 0.867329935942 0.111366671854
1.0324360882 1.12175173111 0.0893156429109

另外一次运行结果:
复制代码 代码如下:

4.05223164501e-07 0.565988971446 0.565988566223
0.768132520942 0.843723660494 0.0755911395524
1.04367819446 1.13247636739 0.0887981729298

在运行结果中,第三列表示插入数据使用的时间。综合看来,方法insert1()的速度很慢,原因在于每次insert都commit()。

更安全地操作数据库

先上代码:

复制代码 代码如下:

import sqlite3

def create_tables(dbname): 
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''create table userinfo(name text, email text)''')
    conn.commit()
    cursor.close()
    conn.close()

def drop_tables(dbname):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.execute('''drop table userinfo''')
    conn.commit()
    cursor.close()
    conn.close()

def insert():
    users = [('qq','qq@example.com'),
            ('ww','ww@example.com'),
            ('ee','ee@example.com'),
            ('rr','rr@example.com'),
            ('tt','tt@example.com'),
            ('yy','yy@example.com'),
            ('uu','uu@example.com')
            ]
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    cursor.executemany("insert into userinfo(name, email) values(?, ?)", users)
    conn.commit()
    cursor.close()
    conn.close()

def insecure_select(text):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    print "select name from userinfo where email='%s'" % text
    for row in cursor.execute("select name from userinfo where email='%s'" % text):
        print row
def secure_select(text):
    conn = sqlite3.connect(dbname)
    cursor = conn.cursor()
    print "select name from userinfo where email='%s'" % text
    for row in cursor.execute("select name from userinfo where email= ? ", (text,)):
        print row

if __name__ == '__main__':
    dbname = 'test.db'
    create_tables(dbname)
    insert()
    insecure_select("uu@example.com")
    insecure_select("' or 1=1;--")
    secure_select("uu@example.com")
    secure_select("' or 1=1;--")
    drop_tables(dbname)


运行结果:
复制代码 代码如下:

select name from userinfo where email='uu@example.com'
(u'uu',)
select name from userinfo where email='' or 1=1;--'
(u'qq',)
(u'ww',)
(u'ee',)
(u'rr',)
(u'tt',)
(u'yy',)
(u'uu',)
select name from userinfo where email='uu@example.com'
(u'uu',)
select name from userinfo where email='' or 1=1;--'

函数insecure_select(text)和secure_select(text)的本意都是根据email获取对应的用户名信息。但是insecure_select(text)的实现容易引起sql注入。

insecure_select("' or 1=1;--")便是一个例子。在insecure_select()中cursor.execute()只有一个参数,即sql语句,这个生成的sql语句如果有问题,还是会照常执行。

secure_select(text)的实现可以防止sql注入,cursor.execute()的第一个参数使用了占位符?表示要被替代的内容,第二个参数指定每个占位符对应的值,在底层实现上,这种方法(至少)转义了特殊字符,可以防止sql注入。

相关文章

  • Python实现遍历读取文件或文件夹

    Python实现遍历读取文件或文件夹

    搞机器学习或者深度学习算法很多时候需要遍历某个目录读取文件,特别是经常需要读取某个特定后缀的文件。本文为大家准备了Python遍历读取文件或文件夹的示例代码,需要的可以参考一下
    2022-08-08
  • Python常见库matplotlib学习笔记之画图文字的中文显示

    Python常见库matplotlib学习笔记之画图文字的中文显示

    在Python中使用matplotlib或者plotnine模块绘图时,常常出现图表中无法正常显示中文的问题,下面这篇文章主要给大家介绍了关于Python常见库matplotlib学习笔记之画图文字的中文显示的相关资料,需要的朋友可以参考下
    2023-05-05
  • 使用Python中PDB模块中的命令来调试Python代码的教程

    使用Python中PDB模块中的命令来调试Python代码的教程

    这篇文章主要介绍了使用Python中PDB模块中的命令来调试Python代码的教程,包括设置断点来修改代码等、对于Python团队项目工作有一定帮助,需要的朋友可以参考下
    2015-03-03
  • Pandas保存csv数据的三种方式详解

    Pandas保存csv数据的三种方式详解

    CSV文件以纯文本形式存储表格数据(数字和文本),是一种通用的、相对简单的文件格式,被用户、商业和科学广泛应用。本文介绍了三种Pandas保存CSV文件数据的方法,需要的可以参考一下
    2022-03-03
  • python实现杨氏矩阵查找

    python实现杨氏矩阵查找

    这篇文章主要为大家详细介绍了Python实现杨氏矩阵查找,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • Python爬虫Scrapy框架CrawlSpider原理及使用案例

    Python爬虫Scrapy框架CrawlSpider原理及使用案例

    这篇文章主要介绍了Python爬虫Scrapy框架(CrawlSpider),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • pycharm实现增加运行时内存

    pycharm实现增加运行时内存

    这篇文章主要介绍了pycharm实现增加运行时内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Python修改文件往指定行插入内容的实例

    Python修改文件往指定行插入内容的实例

    今天小编就为大家分享一篇Python修改文件往指定行插入内容的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • 使用selenium模拟动态登录百度页面的实现

    使用selenium模拟动态登录百度页面的实现

    本文主要介绍了使用selenium模拟动态登录百度页面,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Python文件和流(实例讲解)

    Python文件和流(实例讲解)

    下面小编就为大家带来一篇Python文件和流(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09

最新评论