sql - Python Peewee execute_sql() example -
i using peewee module orm project.
i read entire documentation, there no clear example on how process result db.execute_sql().
i traced code, can find db.execute_sql() return cursor.
does knows how process cursor, such iterate on , result complex select statement.
update: found following source code peewee folder, should me resolve problem.
class queryresultwrapper(object): """ provides iterator on results of raw query, additionally doing 2 things: - converts rows database python representations - ensures multiple iterations not result in multiple queries """ def __init__(self, model, cursor, meta=none): self.model = model self.cursor = cursor self.__ct = 0 self.__idx = 0 self._result_cache = [] self._populated = false self._initialized = false if meta not none: self.column_meta, self.join_meta = meta else: self.column_meta = self.join_meta = none def __iter__(self): self.__idx = 0 if not self._populated: return self else: return iter(self._result_cache) def process_row(self, row): return row def iterate(self): row = self.cursor.fetchone() if not row: self._populated = true raise stopiteration elif not self._initialized: self.initialize(self.cursor.description) self._initialized = true return self.process_row(row) def iterator(self): while true: yield self.iterate() def next(self): if self.__idx self.__ct): try: self.next() except stopiteration: break
peewee returns cursor. can use db-api 2 iterate on it:
cursor = db.execute_sql('select * tweets;') row in cursor.fetchall(): print row cursor = db.execute_sql('select count(*) tweets;') res = cursor.fetchone() print 'total: ', res[0]
Comments
Post a Comment