Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實(shí)例
table通過(guò)使用下面語(yǔ)句創(chuàng)建:
更快地插入數(shù)據(jù)
在此用time.clock()來(lái)計(jì)時(shí),看看以下三種方法的速度。
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é)果:
4.05223164501e-07 0.531585119557 0.531584714334
0.755963264089 0.867329935942 0.111366671854
1.0324360882 1.12175173111 0.0893156429109
另外一次運(yùn)行結(jié)果:
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ù)
先上代碼:
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é)果:
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安裝Scrapy庫(kù)的常見(jiàn)報(bào)錯(cuò)解決
本文主要介紹了Python安裝Scrapy庫(kù)的常見(jiàn)報(bào)錯(cuò)解決,文中通過(guò)圖文示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11基于Python的Houdini插件開(kāi)發(fā)過(guò)程詳情
這篇文章主要介紹了基于Python的Houdini插件開(kāi)發(fā)過(guò)程詳情,Houdini是基于QT進(jìn)行的開(kāi)發(fā),支持?Python、HScript二種腳本進(jìn)行插件開(kāi)發(fā),下面文章介紹內(nèi)容,需要的朋友可以參考一下2022-02-02python?pyvis庫(kù)創(chuàng)建可視化交互式網(wǎng)絡(luò)圖
這篇文章主要為大家介紹了python?pyvis庫(kù)創(chuàng)建可視化交互式網(wǎng)絡(luò)圖,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python可變參數(shù)*args和**kwargs用法實(shí)例小結(jié)
這篇文章主要介紹了Python可變參數(shù)*args和**kwargs用法,結(jié)合實(shí)例形式總結(jié)分析了Python中可變參數(shù)*args和**kwargs的功能、區(qū)別與具體使用技巧,需要的朋友可以參考下2018-04-04Python利用多進(jìn)程將大量數(shù)據(jù)放入有限內(nèi)存的教程
這篇文章主要介紹了Python利用多進(jìn)程將大量數(shù)據(jù)放入有限內(nèi)存的教程,使用了multiprocessing和pandas來(lái)加速內(nèi)存中的操作,需要的朋友可以參考下2015-04-04理解python多線程(python多線程簡(jiǎn)明教程)
這篇文章主要介紹了理解python多線程,一個(gè)快速理解python多線程的簡(jiǎn)明教程,需要的朋友可以參考下2014-06-06