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

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

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

通过本文主要向大家介绍了python3 sqlite3,python sqlite3,python连接sqlite3,sqlite3 实例,sqlite3 数据类型等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com


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)
</div>

某次运行结果:

更安全地操作数据库

先上代码:

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(

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

  • 使用Python对SQLite数据库操作
  • Python简单操作sqlite3的方法示例
  • SQLite3中文编码 Python的实现
  • SQLite3中文编码 Python的实现
  • 详解Python 数据库 (sqlite3)应用
  • Python解析excel文件存入sqlite数据库的方法
  • python从sqlite读取并显示数据的方法
  • python实现在sqlite动态创建表的方法
  • python查询sqlite数据表的方法
  • 在Python中使用SQLite的简单教程

相关文章

  • 用python删除java文件头上版权信息的方法
  • python3中str(字符串)的使用教程
  • 在Python中用get()方法获取字典键值的教程
  • python文件的md5加密方法
  • 在Python的Django框架上部署ORM库的教程
  • python基础教程之基本数据类型和变量声明介绍
  • 浅要分析Python程序与C程序的结合使用
  • python登录豆瓣并发帖的方法
  • python基础教程之实现石头剪刀布游戏示例
  • Python yield使用方法示例

文章分类

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

最近更新的内容

    • Python使用稀疏矩阵节省内存实例
    • Python yield 小结和实例
    • KMP算法精解及其Python版的代码示例
    • Python中的index()方法使用教程
    • 在Python中使用NLTK库实现对词干的提取的教程
    • python网络编程之文件下载实例分析
    • Python的设计模式编程入门指南
    • Python的Django框架中TEMPLATES项的设置教程
    • Python中使用dom模块生成XML文件示例
    • python使用socket向客户端发送数据的方法

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

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