Archived:Database in PySymbian
- e32db Module
- e32dbm Module
This interface in PySymbian provides a low level access to database. Its supports transaction and querying with the use of Simple Query Language . This Module can be very useful for applications that needs to store relationally organized data and want to perform queries frequently. In the e32db case the database is local more than a real thats why this module is not frequently used.
In most cases the e32dbm module is used the reason for this is e32dbm module is very simple and it supports a lots of dictionary functions like adding, deleting the key-value pairs. The code snippet will make it more clear about this module. e32dbm module actually is a mixture of file and a dictionary. It is simply opened as file and operated as a dictionary.
import e32dbm #import the module
file_nam = u"e:\\python\\wiki.db" #assign the name of the file to a variable
def write(): #define the write function to write in a database
db = e32dbm.open(file_nam,"c") #open the file
db[u"name"] = u" Gargi Das "
db[u"place"] = u"Bhavnagar"
db[u"Country"] = u"india"
def read(): #define a read function to read a database
db = e32dbm.open(file_nam,"r") #open a file
for key, value in db.items(): #read it using the dictionary concept.
print "KEY", key, "VALUES", value
print "writing database......................."
print "reading database......................."
the code above seems to be very self explanatory and one thing to mention is the modes in which we can access different database files.
Modes of accessing a database file
The following are the modes in which one can open a database file.
- r - opens a database file for reading only.
- w - opens a database file for reading and writing.
- c - opens a database file for reading and writing. It creates a new database file if the file doesn't exist.
- n - opens a database for reading and writing. It creates and empty database if the database doesn't exist or clears the previous one.