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

本文详细介绍了如何在Python中使用SQLite进行数据库操作,包括连接数据库、创建表、插入数据、查询数据(包括筛选和排序)、更新和删除数据等基本操作。
摘要由CSDN通过智能技术生成

创建与关闭

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知来者逆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值