Python SQLite 简易教程

数据类型

支持的数据类型
https://www.sqlite.org/datatype3.html

时间类型
https://www.sqlite.org/lang_datefunc.html

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding:utf-8 -*-
import sqlite3
conn = sqlite3.connect('filtersys.db')
cursor = conn.cursor()
# 创建表
cursor.execute('create table bad_behaviors (id INTEGER primary key AUTOINCREMENT, devid INTEGER, date NUMERIC)' )
# 插入数据
devid = 15
print "insert into bad_behaviors (devid, date) select " + str(devid) + ",date('now','localtime')"
cursor.execute("insert into bad_behaviors (devid, date) select ?,datetime('now','localtime')", (devid, ))
devid = 18
cursor.execute("insert into bad_behaviors (devid, date) select ?,datetime('now','localtime')", (devid, ))
# 查询
cursor.execute("select * from bad_behaviors where devid=18")
rs = cursor.fetchall()
print rs
cursor.close()
conn.commit()
conn.close()

参考资料

[1] 简易教程
[2] 廖雪峰sqlite
[3] 官方文档