In your lib/app_globals.py set up a database pool (using DBUtils).

eg.

         self.pool = PooledDB(MySQLdb,
                              int(app_conf['db.pool_size']),
                              user=app_conf['db.user'],
                              passwd=app_conf['db.password'],
                              host=app_conf['db.host'],
                              db=app_conf['db.name'],
                              cursorclass=MySQLdb.cursors.DictCursor)

Then you can access a db connection from the pool in your controllers 
using the 'g' variable:

con = g.pool.connection()
cur = con.cursor()
cur.execute("select * from foo")
results = cur.fetchall()

You can ofcourse encapsulate this in your Sql class.

Hope that helps,

Phil


On Tue, 21 Jul 2009, ????? ?????? wrote:

>
> I have my little class for working with MySQL:
> # -*- coding: utf-8 -*-
>
> import MySQLdb
>
> class Sql(object):
>       __db=None
>       __cur=None
>       __numRows=None
>
>       def __init__(self,conf):
>               try:
>                       
> self.__db=MySQLdb.connect(host=conf["host"],user=conf["user"],\
>                                               
> passwd=conf["passwd"],db=conf["db"],\
>                                               
> use_unicode=False,charset=conf["charset"])
>               except:
>                       self.__db=False
>
>       def query(self,query):
>               if self.__db:
>                       self.__cur=self.__db.cursor(MySQLdb.cursors.DictCursor)
>                       try:
>                               self.__numRows=self.__cur.execute(query)
>                               return True
>                       except:
>                               self.__cur=False
>                               return False
>               else:
>                       self.__cur=False
>                       return False
>       def escapeStr(self,s):
>               if self.__db:
>                       #Decoding and encoding for little pylons utf bug :)
>                       return 
> self.__db.escape_string(s.encode('utf-8','replace')).decode
> ("utf-8")
>               else:
>                       return False
>
>       def begin(self):
>               if self.__db:
>                       self.__db.begin()
>               else:
>                       return False
>
>       def commit(self):
>               if self.__db:
>                       self.__db.commit()
>               else:
>                       return False
>
>       def rollback(self):
>               if self.__db:
>                       self.__db.rollback()
>               else:
>                       return False
>
>       def lastId(self):
>               if self.__cur and self.__db:
>                       return self.__db.insert_id()
>               else:
>                       return False
>
>       def getNumRows(self):
>               if self.__cur and self.__db:
>                       return self.__numRows
>               else:
>                       return False
>
>       def getResult(self):
>               if self.__cur and self.__db:
>                       return self.__cur.fetchall()
>               else:
>                       return False
>
>       def __str__(self):
>               return "db: %s,cursor: %s, numRows %s" %
> (self.__db,self.__cur,self.__numRows)
>
> How can i use in Pylons?
>
> >

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to