• 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实现排序算法
  • python pdb调试方法分享
  • 基于asyncio 异步协程框架实现收集B站直播弹幕
  • 详解Python中的Descriptor描述符类
  • python基础教程之popen函数操作其它程序的输入和输出示例
  • wxpython 学习笔记 第一天
  • python模拟新浪微博登陆功能(新浪微博爬虫)
  • Python 类与元类的深度挖掘 I【经验】
  • python实现提取百度搜索结果的方法
  • Python getopt模块处理命令行选项实例

文章分类

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

最近更新的内容

    • Python CSV模块使用实例
    • 解读Python中degrees()方法的使用
    • 利用Python的Django框架中的ORM建立查询API
    • python logging类库使用例子
    • Python字符串特性及常用字符串方法的简单笔记
    • 简单介绍Python下自己编写web框架的一些要点
    • Python实现的最近最少使用算法
    • python通过floor函数舍弃小数位的方法
    • Python标准库之循环器(itertools)介绍
    • python基础知识小结之集合

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

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