6.3. Local Database

Symbian OS includes a relational database engine, to which PyS60 provides two interfaces. The first interface can be found in module e32db and the second interface in module e32dbm.

The former interface provides low-level, versatile access to the database. It supports transactions and querying the database with a subset of Simple Query Language (SQL). This interface can be useful for applications that need to store relationally organized data and perform queries frequently. In this case, having a local database, instead of sending data to a 'real' database on a server, may be justified.

In many cases, the interface provided in module e32dbm is preferred because of its simplicity. In essence, you can treat e32dbm as a persistent dictionary for string keys and values. It supports most of the standard dictionary functions, such as adding, deleting and iterating through key–value pairs.

Example 6.9. Local database
import e32dbm

DB_FILE = u"c:\\python\\test.db"

def write_db():
    db = e32dbm.open(DB_FILE, "cf")
    db[u"host"] = u"www.google.com"
    db[u"port"] = u"80"
    db[u"username"] = u"musli"
    db[u"password"] = u"my secret"
    db.close()

def read_db():
    db = e32dbm.open(DB_FILE, "r")
    for key, value in db.items():
        print "KEY", key, "VALUE", value
    db.close()

print "Writing db.."
write_db()
print "Reading db.."
read_db()

As you can see, the local database module, e32dbm, behaves like a mixture of a file and a dictionary: it is opened like a file and accessed like a dictionary. ...

Get Mobile Python: Rapid Prototyping of Applications on the Mobile Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.