最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實(shí)例

 更新時(shí)間:2014年04月26日 09:24:07   作者:  
這篇文章主要介紹了Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實(shí)例,通過(guò)在一個(gè)表格中進(jìn)行操作來(lái)論述如何使用Python快速安全地操作sqlite3,需要的朋友可以參考下


table通過(guò)使用下面語(yǔ)句創(chuàng)建:

復(fù)制代碼 代碼如下:
create table userinfo(name text, email text)

更快地插入數(shù)據(jù)

在此用time.clock()來(lái)計(jì)時(shí),看看以下三種方法的速度。

復(fù)制代碼 代碼如下:

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)

某次運(yùn)行結(jié)果:

復(fù)制代碼 代碼如下:

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

另外一次運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:

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

在運(yùn)行結(jié)果中,第三列表示插入數(shù)據(jù)使用的時(shí)間。綜合看來(lái),方法insert1()的速度很慢,原因在于每次insert都commit()。

更安全地操作數(shù)據(jù)庫(kù)

先上代碼:

復(fù)制代碼 代碼如下:

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)


運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:

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;--'

函數(shù)insecure_select(text)和secure_select(text)的本意都是根據(jù)email獲取對(duì)應(yīng)的用戶名信息。但是insecure_select(text)的實(shí)現(xiàn)容易引起sql注入。

insecure_select("' or 1=1;--")便是一個(gè)例子。在insecure_select()中cursor.execute()只有一個(gè)參數(shù),即sql語(yǔ)句,這個(gè)生成的sql語(yǔ)句如果有問(wèn)題,還是會(huì)照常執(zhí)行。

secure_select(text)的實(shí)現(xiàn)可以防止sql注入,cursor.execute()的第一個(gè)參數(shù)使用了占位符?表示要被替代的內(nèi)容,第二個(gè)參數(shù)指定每個(gè)占位符對(duì)應(yīng)的值,在底層實(shí)現(xiàn)上,這種方法(至少)轉(zhuǎn)義了特殊字符,可以防止sql注入。

相關(guān)文章

  • Python提取PDF發(fā)票信息保存Excel文件并制作EXE程序的全過(guò)程

    Python提取PDF發(fā)票信息保存Excel文件并制作EXE程序的全過(guò)程

    之前零散的用過(guò)一點(diǎn)python做數(shù)據(jù)處理,這次又遇到一個(gè)數(shù)據(jù)處理的小功能,下面這篇文章主要給大家介紹了關(guān)于Python提取PDF發(fā)票信息保存Excel文件并制作EXE程序的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • 基于Python實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物檢測(cè)功能

    基于Python實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物檢測(cè)功能

    基于dlib庫(kù)的模型,實(shí)現(xiàn)人臉識(shí)別和焦點(diǎn)人物的檢測(cè)。最后呈現(xiàn)的效果為焦點(diǎn)人物的識(shí)別框顏色與其他人物框不一樣。對(duì)Python人臉識(shí)別和焦點(diǎn)人物檢測(cè)設(shè)計(jì)過(guò)程感興趣的朋友一起看看吧
    2021-10-10
  • pytorch + visdom 處理簡(jiǎn)單分類問(wèn)題的示例

    pytorch + visdom 處理簡(jiǎn)單分類問(wèn)題的示例

    這篇文章主要介紹了pytorch + visdom 處理簡(jiǎn)單分類問(wèn)題的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Python之try無(wú)法使用全局變量的問(wèn)題解決

    Python之try無(wú)法使用全局變量的問(wèn)題解決

    當(dāng)我們使用try語(yǔ)句時(shí),如果在try中使用了全局變量,但又在except或finally中修改了這個(gè)全局變量,就會(huì)出現(xiàn)這種無(wú)法修改全局變量的情況,下面就來(lái)解決一下這個(gè)問(wèn)題,感興趣的可以了解一下
    2024-08-08
  • 完美解決Django2.0中models下的ForeignKey()問(wèn)題

    完美解決Django2.0中models下的ForeignKey()問(wèn)題

    這篇文章主要介紹了完美解決Django2.0中models下的ForeignKey()問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • Python 函數(shù)詳解:從基礎(chǔ)語(yǔ)法到高級(jí)使用技巧

    Python 函數(shù)詳解:從基礎(chǔ)語(yǔ)法到高級(jí)使用技巧

    本文基于實(shí)例代碼,全面講解 Python 函數(shù)的定義、參數(shù)傳遞、變量作用域及類型標(biāo)注等知識(shí)點(diǎn),幫助初學(xué)者快速掌握函數(shù)的使用技巧,感興趣的朋友跟隨小編一起學(xué)習(xí)吧
    2025-08-08
  • Python requests請(qǐng)求超時(shí)的解決方案

    Python requests請(qǐng)求超時(shí)的解決方案

    在進(jìn)行網(wǎng)絡(luò)數(shù)據(jù)爬取過(guò)程中,網(wǎng)絡(luò)請(qǐng)求超時(shí)是一個(gè)令人頭疼的問(wèn)題,尤其在Python中,我們常常需要應(yīng)對(duì)各種網(wǎng)絡(luò)爬蟲(chóng)、API調(diào)用或其他網(wǎng)絡(luò)操作,而網(wǎng)絡(luò)請(qǐng)求超時(shí)的原因千奇百怪,在本篇文章中,我們將深入探討Python requests請(qǐng)求超時(shí)的解決方案,需要的朋友可以參考下
    2024-12-12
  • python類屬性學(xué)習(xí)深入講解

    python類屬性學(xué)習(xí)深入講解

    這篇文章主要介紹了python類屬性學(xué)習(xí)深入講解,文中對(duì)于python的類屬性的理解有正在學(xué)習(xí)python的同學(xué)可以一塊學(xué)習(xí)下
    2021-03-03
  • python獲得圖片base64編碼示例

    python獲得圖片base64編碼示例

    這篇文章主要介紹了用python語(yǔ)言獲得圖片的Base64編碼的示例,大家參考使用吧
    2014-01-01
  • Python如何獲取多線程返回結(jié)果

    Python如何獲取多線程返回結(jié)果

    這篇文章主要介紹了Python如何獲取多線程返回結(jié)果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評(píng)論

天峨县| 获嘉县| 溧阳市| 朝阳市| 宜州市| 花莲县| 保靖县| 同仁县| 璧山县| 扎囊县| 饶阳县| 四平市| 新乡市| 翁源县| 阳新县| 黄石市| 揭阳市| 郁南县| 元江| 阿瓦提县| 开鲁县| 疏附县| 普格县| 深泽县| 高密市| 玉环县| 白山市| 赫章县| 黄冈市| 皮山县| 庆元县| 彰化市| 鹿泉市| 黑河市| 琼中| 徐州市| 汉中市| 望奎县| 华亭县| 江山市| 兴城市|