robot
最新文章(10)
Mqskit 和其它相關工具
CPython 的 GC 二、三事
寫 Mecurial Extension 是件快樂的事!
Mozilla 台灣辨公室徵人啟事
關於 Apple 的兩項專利
core dump 之前的 frame
怎麼發出 beep 聲?
先承認你要找的是奴才吧!
程式碼要清的多乾淨?
FreeBSD 的 Thread-Local Storage 實作
首頁
新編
最新留言
Entries RSS
重要關鍵字(10)
coding (122)
Python (93)
FreeBSD (71)
WEB (61)
URL (48)
hardware (46)
javascript (36)
Linux (34)
blog (30)
C++ (16)
所有關鍵字
新增 URL
PySQL
by thinker
2 Columns
關鍵字:
Python
coding
之前 linkname:[Host SQL in $Python$] http://heaven.branda.to/~thinker/GinGin_CGI.py/show_id_doc/108 與 linkname:[Host SQL in $Python$ II] http://heaven.branda.to/~thinker/GinGin_CGI.py/show_id_doc/162 兩篇討論以 $Python$ implement SQL command 的文章,被我丟到倉庫一陣子後,因為某 project 而被找回來。目前已經和 sqlite 進行整合,已可使用 $Python$ 定義 table 、 query 、 update 和 insert 。 下面是 PySQL 的一個$範例$: {{{ from pysqlite2 import dbapi2 as sqlite from pysql import * # # define data source # comprises tables and queries # class my_data_src(data_src): def ds_definition(): class t1(pysql.table): # # define table 't1' # item = pysql.str_f() count = pysql.int_f() pass class t2(pysql.table): # # define table 't2' # item = pysql.str_f() money = pysql.float_f() pass # # define a query 'get_count_and_money_of_item' # get_count_and_money_of_item = \ lambda: (t1 * t2) \ .fields(t1.item, t1.count * t2.money - 30) \ .where((t1.item == t2.item) & (t1.item != pysql._q)) return ds_define() pass cx = sqlite.connect('$test$.db') db = my_data_src(cx) # create a instance of data source db.init_db() # create tables db.insert(db.t1, item='foo', count=100) db.insert(db.t2, item='foo', money=3.2) db.insert(db.t1, item='boo', count=50) db.insert(db.t2, item='boo', money=3.0) db.update(db.t1, count=140, where=~(db.t1.item == 'foo')) cu = db.get_count_and_money_of_item('foo') rows = cu.fetchall() print rows cu = db.get_count_and_money_of_item('cool') rows = cu.fetchall() print rows db.commit() pass }}} == Downloads == 需要 pysql 和 pythk 兩個 module * http://master.branda.to/downloads/pysql/pysql-20070108.tar.gz * http://master.branda.to/downloads/pythk/pythk-20070108.tar.gz
最後更新時間: 2007-01-08 12:18:06 CST |
引用
查詢:
COMMENTS: