MySql - MySqlデータベースにアクセスするための簡単なMySQLdbラッパ

このモジュールはとてもシンプルなモジュールです。ソースコードは以下の通り:
import MySQLdb

################
CherryClass MySql abstract:
################
function:
    def openConnection(self, host, user, passwd, db):
        self.connection=MySQLdb.connect(host, user, passwd, db)
################
function:
    def openConnection(self, host, user, passwd, db):
        self.connection=MySQLdb.connect(host, user, passwd, db)
    def query(self, query):
        c=self.connection.cursor()
        c.execute(query)
        res=c.fetchall()
        c.close()
        return res

ここで行ったことはPython MySQLdbモジュールへのCherryClassのラッパを提供しただけです。

やるべき事はMySqlから継承したCherryClassを宣言し、openConnectionメソッドを__init__メソッド内で呼び出し、クエリーを実行してその結果を得るためにqueryを使うことです。

サーバが立ち上がる時(CherryClassが実体を得る時)コネクションは自動的に開き、サーバーが死ぬまで開かれたままになります。

以下のコードはこのモジュールの使い方の例です:

use MySql
CherryClass MyDb(MySql):
function:
    def __init__(self):
        self.openConnection('host', 'user', 'password', 'database')

CherryClass Root:
mask:
    def index(self):
        <html><body>
            Hello, there are currently <py-eval="myDb.query('select count(*) from user')[0][0]"> use
rs in the database
        </body></html>


Debian User 2003-10-13