import sqlite3
db = sqlite3.connect("c:/tmp/test2.db") #连接数据库,若不存在则自动创建
#文件夹 c:/tmp 必须事先存在,connect不会创建文件夹
cur = db.cursor() #获取光标,要操作数据库一般要通过光标进行
sql = '''CREATE TABLE if not exists students (id integer primary key,
name text, gpa real, birthday date, age integer, picture blob)''' #如果
表 students 不存在就创建它
cur.execute(sql) #执行SQL命令
cur.execute("insert into students values(1600, '张三', 3.81, '2000-
09-12', 18,null)") #插入一个记录
mylist = [(1700, '李四', "3.25",'2001-12-01',17,None),
(1800, '王五', "3.35",'1999-01-01',19,None)]
for s in mylist: #依次插入mylist中的每个记录
cur.execute('INSERT INTO students VALUES(?,?,?,?,?,?)',(s[0], s[1], s[2],s[3],s[4],s[5])) # ?对应于后面某项
db.commit() #真正写入,写入操作都需要这个
cur.close() #关闭光标
db.close() #关闭数据库
SELECT * FROM students
检索students表中全部记录
SELECT * FROM students ORDER BY age
检索students表中全部记录,并按年龄排序
SELECT name,age FROM students
检索students表中全部记录,但每个记录只取name和age字段
SELECT * FROM students WHERE name = '张三'
检索students表中全部name字段为张三的记录
WHERE 表示检索条件
SELECT * FROM students WHERE name = '张三' AND age > 20 ORDER BY age DESC
检索students表中全部名为张三且年龄大于20的人,结果按年龄降序排列
UPDATE students SET gpa = 3.9
将所有记录的gpa设置成3.9
UPDATE students SET gpa = 3.9, age = 18 WHERE name = '李四'
修改 李四 的gpa和年龄
DELETE FROM students WHERE age < 18
删除年龄小于18的记录
DELETE FROM students
删除全部记录
DROP TABLE IF EXISTS students
删除students 表
检索数据库
例1
import sqlite3
db = sqlite3.connect("c:/tmp/test2.db")
cur = db.cursor()
sql = 'select * from students' #检索全部记录
cur.execute(sql)
x = cur.fetchone() #fetchone 取满足条件的第一条记录
print(x) #=>(1600, '张三', 3.81, '2000-09-12', 18, None)
print(x[1]) #=>张三
for x in cur.fetchall(): #fetchall取得所有满足条件的记录
print(x[:-2]) #age和picture字段不打出
cur.execute("SELECT * FROM students WHERE name='Jack'")
x = cur.fetchone()
if x == None:
print("can't find Jack")
cur.close()
db.close()
例1
import sqlite3
db = sqlite3.connect("c:/tmp/test2.db")
cur = db.cursor()
sql = 'select name, gpa, age from students where gpa > 3.3
order by age desc'
#查找gpa > 3.3的记录,题取其中三个字段,按年龄降序排列
cur.execute(sql)
x = cur.fetchall()
if x != []:
print("total: ", len(x)) #=>2
for r in x:
print(r)
cur.close()
db.close()
== 修改数据库==
import sqlite3
db = sqlite3.connect("c:/tmp/test2.db")
cur = db.cursor()
sql = 'UPDATE students SET gpa = ?, age = ? WHERE name = ?'
cur.execute(sql,(4.0,20,'李四')) #元组三个元素分别对应三个 ? #修改 李四 的gpa和年龄 。若李四不存在,则无效果
db.commit() #写入操作必须
cur.close()
db.close()
删除表:
import sqlite3
db = sqlite3.connect("c:/tmp/test2.db")
cur = db.cursor()
cur.execute("DROP TABLE IF EXISTS students")
db.commit()
try:
cur.execute("select * from students")
x = cur.fetchall()
for r in x:
print(r[:-1])
except:
print("no table") #=> no table
cur.close()
db.close()
列出数据库中所有的表和表的结构
import sqlite3
db = sqlite3.connect("c:/tmp/test3.db")
cur = db.cursor()
sql = 'CREATE TABLE if not exists table2 (id integer, name text)'
cur.execute(sql) #执行SQL命令
sql = 'CREATE TABLE if not exists table1 (id integer, schook text)'
cur.execute(sql)
db.commit()
cur.execute('select name from SQLITE_MASTER where type="table" order by NAME')
x = cur.fetchall()
if x != []:
print(x)
cur.execute("PRAGMA TABLE_INFO (table1)")
print (cur.fetchall())
cur.close()
db.close()
设置blob字段(二进制字段)的值:
import sqlite3
import requests
f = open('c:/tmp/tmp.jpg','rb') #二进制方式打开图片
img = f.read()
f.close()
db = sqlite3.connect("c:/tmp/test2.db")
cur = db.cursor()
sql = "UPDATE students SET picture=? WHERE name = '李四'"
cur.execute(sql,(img,)) #设置李四的照片。 img对应于 ?