Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 01:02, 31 May 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:A simple ORM SQL for PySymbian

From Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

Article Metadata
Created: cyke64 (31 Mar 2007)
Last edited: hamishwillee (31 May 2013)

This simple Object Relational Mapper is adapted from SQLAlchemy and SQLObject. You need to create a database manually first.

from __future__ import generators
import e32db, re
db = e32db.Dbms()
dbv = e32db.Db_view()'C:\\test.db')
# Some helping classes (need more in next version)
class String:
class Integer:
class Float:
class column:
def __init__(self, coltype):
self.coltype = coltype
class Mapper(object):
def __init__(self, id=None, **kw):
if id is None: = self._insert(**kw)
else: = id
def _insert(self, **kw):
names = ','.join(kw.keys())
values = ','.join(self.quote(k,v) for k,v in kw.items())
tablename = self.__class__.__name__
q = u"INSERT INTO %s(%s) VALUES (%s)" % (tablename, names, values)
# get last insert ID
dbv.prepare(db, u'SELECT id FROM '+tablename+' ORDER BY id DESC')
return dbv.col(1)
def __getattr__(self, name):
if hasattr(self.mapping, name):
q = 'SELECT '+name+' FROM '+self.__class__.__name__
q += ' WHERE id='+str(
dbv.prepare(db, unicode(q))
return dbv.col(1)
return self.__dict__[name]
def __repr__(self):
return '<%s id=%d>' % (self.__class__.__name__,
def quote(self, name, value):
if getattr(self.mapping, name).coltype == String:
return "'%s'" % value.replace("'", "''") # encode single quote
return str(value)
def __setattr__(self, name, value):
if hasattr(self.mapping, name):
q = 'UPDATE '+self.__class__.__name__+' SET '+name+'='
q += self.quote(name, value) + " WHERE id=" + str(
self.__dict__[name] = value
def set(self, **kw):
q = "UPDATE "+self.__class__.__name__+" SET "
for k, v in kw.items():
q += k+'='+self.quote(k,v)+','
q = q[:-1]+" WHERE id=%s" %
def delete(self):
q = 'DELETE FROM '+self.__class__.__name__+" WHERE id=" + str(
db.execute(unicode(q)) = None
def dict(self):
names = [k for k in self.mapping.__dict__ if not k.startswith('__')]
q = 'SELECT '+','.join(names)+' FROM '+self.__class__.__name__
q += ' WHERE id=' + str(
dbv.prepare(db, unicode(q))
dct = {'id':}
for i in range(dbv.col_count()):
dct[names[i]] = dbv.col(i+1)
return dct
def select(cls, where=None, orderby=None):
q = 'SELECT id FROM '+cls.__name__
if where:
q += ' WHERE '+where
if orderby:
q += ' ORDER BY '+orderby
dbv = e32db.Db_view() # need its own cursor
dbv.prepare(db, unicode(q))
for i in range(dbv.count_line()):
yield cls(dbv.col(1))
select = classmethod(select)

Here's how you create your class.

class Phone(Mapper):
class mapping:
# doesn't need id = column(Integer)
name = column(String)
edition = column(Integer)
This page was last modified on 31 May 2013, at 01:02.
39 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.