Thursday, 12 September 2013

sqlalchemy and how to count row

sqlalchemy and how to count row

I have the following code:
query = self.dbObj.session.query(MaterialsTable)
rowCounter = self.dbObj.session.query(MaterialsTable)
for attr , val in interfaceCodesObject.filterNameDict.items():
query = query.filter(and_(getattr(MaterialsTable,attr).like("%%%s%%" %
val)))
rowCounter =
rowCounter.filter(and_(getattr(MaterialsTable,attr).like("%%%s%%" %
val))).count()
query.all()
When i use mutiple field, i get the following traceback:
Traceback (most recent call last):
File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFindFrame.py", line
202, in <lambda>
QtCore.QObject.connect(self.pushButtonSearch,
QtCore.SIGNAL(_fromUtf8("clicked()")), lambda:
self.interfaceCodesConstructor.responseToRequestForData(self))
File "/home/mohsen/codes/amlak/amlak/src/ui/interface/interface.py",
line 109, in responseToRequestForData
self.materialsObejct.findData(self.objectSearchMaterials,self)
File "/home/mohsen/codes/amlak/amlak/src/materials/materials.py", line
133, in findData
rowCounter =
rowCounter.filter(and_(getattr(MaterialsTable,attr).like("%%%s%%" %
val))).count()
AttributeError: 'long' object has no attribute 'filter'
But when i use just one field it use rowCounter as integer and real return.
My question is , how can i adapt it with multiple field?Such as : Where
name = 'barak' and id != 3 But one filed such as : Where name = 'barak'

No comments:

Post a Comment