ホームページ >バックエンド開発 >Python チュートリアル >Python で sqlite3 を操作してデータを迅速かつ安全に挿入する例 (アンチインジェクション)
table通过使用下面语句创建:
更快地插入数据
在此用time.clock()来计时,看看以下三种方法的速度。
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)
某次运行结果:
更安全地操作数据库
先上代码:
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)
函数insecure_select(text)和secure_select(text)的本意都是根据email获取对应的用户名信息。但是insecure_select(text)的实现容易引起sql注入。
insecure_select("' or 1=1;--") は一例です。 insecure_select() では、cursor.execute() のパラメータは 1 つだけで、生成された SQL ステートメントに問題がある場合でも通常どおり実行されます。
secure_select(text) の実装により、cursor.execute() の最初のパラメータは、置換されるコンテンツを示すために使用されます。基礎となる実装では、このメソッドは (少なくとも) 特殊文字をエスケープし、SQL インジェクションを防ぐことができます。