由於自從Python 2.5推出之後,它就內含了「SQLite (lightweight disk-based database)」,它是一個C library~ 所以有相當多的平台都將它當作內部的儲存方式,好比如:Google Android、Adobe AIR、Google Gears..
所以這篇主要記錄一些簡單的操作~ 感覺有點像一般小型ASP+Access網站的架構模式~
範例如下:
db.py
from mod_python import apache, psp
import sqlite3
def index(req):
req.content_type = 'text/html'
conn = sqlite3.connect('data.dat')
c = conn.cursor()
c.execute('''
create table members(
id int,
name varchar,
login timestamp
)
''')
c.execute('''
insert into members
values (1,'jeff','2006-10-01')
''')
c.execute('''
insert into members
values (2,'angie','2006-10-02')
''')
c.execute('''
insert into members
values (3,'dylan','2006-10-03')
''')
conn.commit()
c.execute('select * from members')
res = c.fetchall()
who = "jeff"
c.execute("select id,name,login from members where name=:who", {"who": who})
jeff = c.fetchone()
template = psp.PSP(req, filename='db.tmpl')
template.run({'results':res,'jeff':jeff})
db.tmpl
<html> <p><%=jeff%></p> <% for f in results: %> <%=f[0]%><%=f[1]%><%=f[2]%><br/> <% %> </html>
相關資源
