Python SQLite数据库使用示范与性能优化的建议

2024-03-31 00:20

本文主要是介绍Python SQLite数据库使用示范与性能优化的建议,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

创建与关闭

Python SQLite 最基本的建立 DB 数据库连接与关闭 DB 数据库。

下面要建立 tutorial.db 这个 database 连接:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
# ...
con.close()

CREATE 用法

CREATE 语法新建数据表:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
# 新建数据表
cur.execute("CREATE TABLE movie(title, year, score)")
con.commit()
con.close()

INSERT 用法与

SQLite INSERT 语法新增/插入数据:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
cur.execute("INSERT INTO movie VALUES('Monty Python and the Holy Grail', 1975, 8.2),('And Now for Something Completely Different', 1971, 7.5)")
con.commit()  # 记得在执行 INSERT 后提交事务
con.close()

SQLite INSERT 语法插入多笔数据,插入大量多笔数据可使用 cur.executemany(),在 executemany 参数中可以用 ? 表示之后会带入的数据:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
data = [("Monty Python Live at the Hollywood Bowl", 1982, 7.9), ("Monty Python's The Meaning of Life", 1983, 7.5), ("Monty Python's Life of Brian", 1979, 8.0)]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit()  # 记得在执行 INSERT 后提交事务
con.close()

SELECT 用法

SELECT 语法查询数据,如果要查询 tutorial.db 数据库里有什么数据表的话可以使用下面代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
# 查询数据
ret = cur.execute("SELECT name FROM sqlite_master")
print(ret.fetchall())
con.close()

执行结果如下,

['movie',]

如果要查询 movie 数据表里有什么数据的话可以这样写:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
# 查询数据
ret = cur.execute("SELECT * FROM movie")
print(ret.fetchall())
con.close()

执行结果如下,

('Monty Python and the Holy Grail', 1975, 8.2), ('And Now for Something Completely Different', 1971, 7.5), ('Monty Python Live at the Hollywood Bowl', 1982, 7.9), ('Monty Python's The Meaning of Life', 1983, 7.5), ('Monty Python's Life of Brian', 1979, 8.0)

如果要查询 movie 数据表里 score 栏目的话可以这样写:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
# 查询数据
ret = cur.execute("SELECT score FROM movie")
print(ret.fetchall())
con.close()

执行结果如下,

(8.2,), (7.5,), (7.9,), (7.5,), (8.0,)

如果要查询 movie 数据表里 year 跟 title 栏目的话并且按 year 排序的话可以这样写,

上面是示范 fetchall 一次取得所有结果,以下示范用 for 循环来处理 query 每笔结果的数据,

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):print(row)
con.close()

执行结果如下,

(1971, 'And Now for Something Completely Different')
(1975, 'Monty Python and the Holy Grail')
(1979, 'Monty Python's Life of Brian')
(1982, 'Monty Python Live at the Hollywood Bowl')
(1983, 'Monty Python's The Meaning of Life')

SELECT 搭配 WHERE 语法查询特定条件的数据

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
ret = cur.execute("SELECT title, year FROM movie WHERE title='Monty Python and the Holy Grail'")
ret = cur.execute("SELECT title, year FROM movie WHERE year=1971")
print(ret.fetchall())
ret = ret.fetchone()
if ret is None:print("is None")
else:print(ret)
con.close()

执行结果如下,

('Monty Python and the Holy Grail', 1975)

UPDATE 用法

SQLite UPDATE 语法更新数据,如果 movie 数据表找到 score 分数为 8.2 的数据时,更新它们的 title 与 year,

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
cur.execute("UPDATE movie SET title=?, year=? WHERE score=?", ("12345", 2000, 8.2))
con.commit()
con.close()

如果有多笔符合的话会更新多笔。

DELETE 用法

DELETE 语法删除数据表,如果要删除 movie 数据表里的 score 分数为 8.2 的数据可以这么写:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
cur.execute("DELETE FROM movie WHERE score=?", (8.2,))
con.commit()
con.close()

如果要删除 movie 数据表里的所有数据可以这么写:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
cur.execute("DELETE FROM movie")
con.commit()
con.close()

这篇关于Python SQLite数据库使用示范与性能优化的建议的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/862970

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主

如何免费的去使用connectedpapers?

免费使用connectedpapers 1. 打开谷歌浏览器2. 按住ctrl+shift+N,进入无痕模式3. 不需要登录(也就是访客模式)4. 两次用完,关闭无痕模式(继续重复步骤 2 - 4) 1. 打开谷歌浏览器 2. 按住ctrl+shift+N,进入无痕模式 输入网址:https://www.connectedpapers.com/ 3. 不需要登录(也就是

Python 字符串占位

在Python中,可以使用字符串的格式化方法来实现字符串的占位。常见的方法有百分号操作符 % 以及 str.format() 方法 百分号操作符 % name = "张三"age = 20message = "我叫%s,今年%d岁。" % (name, age)print(message) # 我叫张三,今年20岁。 str.format() 方法 name = "张三"age

关于如何更好管理好数据库的一点思考

本文尝试从数据库设计理论、ER图简介、性能优化、避免过度设计及权限管理方面进行思考阐述。 一、数据库范式 以下通过详细的示例说明数据库范式的概念,将逐步规范化一个例子,逐级说明每个范式的要求和变换过程。 示例:学生课程登记系统 初始表格如下: 学生ID学生姓名课程ID课程名称教师教师办公室1张三101数学王老师101室2李四102英语李老师102室3王五101数学王老师101室4赵六103物理陈

数据库期末复习知识点

A卷 1. 选择题(30') 2. 判断范式(10') 判断到第三范式 3. 程序填空(20') 4. 分析填空(15') 5. 写SQL(25') 5'一题 恶性 B卷 1. 单选(30') 2. 填空 (20') 3. 程序填空(20') 4. 写SQL(30') 知识点 第一章 数据库管理系统(DBMS)  主要功能 数据定义功能 (DDL, 数据定义语

给数据库的表添加字段

周五有一个需求是这样的: 原来数据库有一个表B,现在需要添加一个字段C,我把代码中增删改查部分进行了修改, 比如insert中也添入了字段C。 但没有考虑到一个问题,数据库的兼容性。因为之前的版本已经投入使用了,再升级的话,需要进行兼容处理,当时脑子都蒙了,转不过来,后来同事解决了这个问题。 现在想想,思路就是,把数据库的表结构存入文件中,如xxx.sql 实时更新该文件: CREAT