File: MySql.tex

package info (click to toggle)
cherrypy 0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,324 kB
  • ctags: 1,759
  • sloc: python: 14,411; sh: 6,915; perl: 2,472; makefile: 76
file content (46 lines) | stat: -rwxr-xr-x 1,462 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
\section{\module{MySql} --- Simple MySQLdb wrapper to access a MySql database.}
\declaremodule{standard}{MySql}

This module is a very simple module. The source code is the following:

\begin{verbatim}
import MySQLdb

################
CherryClass MySql abstract:
################
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
\end{verbatim}

All it does is it provides a CherryClass wrapper to the Python MySQLdb module

All you have to do to use it is declare a CherryClass that inherits from MySql, call the \var{openConnection}
method in the \var{__init__} method, and use \var{query} to execute a query and get the result.

The connection will be automatically opened when the server gets started (when your CherryClass gets instantiated), and
it will remain open until the server dies.

The following code is an example on how to use the module:

\begin{verbatim}
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]"> users in the database
        </body></html>
\end{verbatim}