snakedb -MySQL
Ming Hui Huang
ming at 0x01.com
Wed Jun 11 11:46:52 EDT 2003
Hi Salvatore,
First of all thanks for using the snakedb module which I wrote to make
database access easier.
Yes, there is a way to set teh sorting criteria. Basically you can set
the "ORDER BY" string using the SnakeDbTable.setSelectOrder method.
Below is a sample test script that uses gadfly database to illustrate
the sort order criteria:
-------------------------------------------------
#!/usr/bin/env python
from snakedb import *
import sys
from string import lower, join, split
class PhoneBookDb(GadflySnakeDb):
dbDir="/tmp"
dbName="phonebook"
def __init__(self):
GadflySnakeDb.__init__(self,self.dbName, self.dbDir)
def registerAllDbTables(self):
self.registerDbTable(PersonTable())
class PersonTable(SnakeDbTable):
tableName="person"
def __init__(self):
sqlFields={
#sqlfield: fieldtype
'personId':'varchar',
'personObjId':'varchar',
'updatedOnTimeStamp':'varchar',
'lastName':'varchar',
'firstName':'varchar',
'phone':'varchar'
}
SnakeDbTable.__init__(self, self.tableName, sqlFields)
class Person(SnakeDbObj):
def __init__(self, db, detailsDict={}):
SnakeDbObj.__init__(self, db, PersonTable.tableName,
detailsDict)
db=PhoneBookDb()
try:
db.dropDb()
except:
pass
db.createDb()
print "New Phone Book database created!"
personsInfo=[{ 'firstName':'Ming', 'lastName':'Huang',
'phone':'212-228-0933' },
{ 'firstName':'Thanos', 'lastName':'Vassilakis',
'phone':'212-228-1933' },
{ 'firstName':'Sue', 'lastName':'Lee', 'phone':'212-228-6633' },
{ 'firstName':'John', 'lastName':'Lee', 'phone':'212-228-5555' }]
for personInfo in personsInfo:
p=Person(db, personInfo)
p.create()
print "Created person: %s"%p.details
print "\n\nListing of all persons without sorting order: "
query=SnakeDbQuery(snakeDb=db,pageSize=5,pageNumber=1)
person=Person(db)
print "\n\nListing of all persons with sorting order by lastname then
firstname: "
person.dbTable.setSelectOrder("firstName, lastName")
print person.find(query)
--------------------------------------------------------------------
Remember to set the setSelectOrder back to empty string if you no
longer want the sort criteria.
Hope this info is helpful.
Regards,
Ming
Salvatore <artyprog at wanadoo.fr> wrote in message news:<bc51mj$o81$1 at news-reader13.wanadoo.fr>...
> Hello,
>
> Does anybody knows how to make
> enhanced criteria like sorting with snakedb
> without using sql request ?
>
>
> Thanks
>
> Regards
>
> Salvatore
More information about the Python-list
mailing list