欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【脚本语言系列】关于Python数据库处理SQLite数据库,你需要知道的事

程序员文章站 2022-06-01 23:06:10
...

如何使用SQLite数据库

# -*- coding:utf-8 -*-
import sqlite3

conn = sqlite3.connect("enterprise.db")
curs = conn.cursor()
curs.execute('''CREATE TABLE zoo(critter VARCHAR(20) PRIMARY KEY,count INT,damages FLOAT)''')
curs.execute("INSERT INTO zoo VALUES('duck',5, 0.0)")
curs.execute("INSERT INTO zoo VALUES('bear',2, 1000.0)")
ins = "INSERT INTO zoo (critter, count, damages) VALUES(?,?,?)"
curs.execute(ins, ("weasel", 1, 2000.0))
curs.execute("SELECT * FROM zoo")
rows = curs.fetchall()
print(rows)
curs.execute("SELECT * from zoo ORDER BY count")
rows = curs.fetchall()
print(rows)
curs.execute("SELECT * from zoo ORDER BY count DESC")
rows = curs.fetchall()
print(rows)
curs.execute('''SELECT * FROM zoo WHERE damages = (SELECT MAX(damages) FROM zoo)''')
rows = curs.fetchall()
print(rows)
curs.close()
conn.close()
[(u'duck', 5, 0.0), (u'bear', 2, 1000.0), (u'weasel', 1, 2000.0)]
[(u'weasel', 1, 2000.0), (u'bear', 2, 1000.0), (u'duck', 5, 0.0)]
[(u'duck', 5, 0.0), (u'bear', 2, 1000.0), (u'weasel', 1, 2000.0)]
[(u'weasel', 1, 2000.0)]

Introducing Python;[美]Bill Lubanovic;2016
Python 语言及其应用;【美】Bill Lubanovic;2016