×
Namespaces

Variants
Actions

Archived:Working database example for e32db in PySymbian

From Nokia Developer 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
Article
Created: User:Mattstabeler (29 Oct 2009)
Last edited: hamishwillee (31 May 2013)

Based on Archived:Complete SQL database guide for PySymbian and Archived:Database in PySymbian, the following example creates a local e32db database, inserts data using SQL, then queries for data using SQL. It also does basic exception handling.

# Working Example for using e32db, 
# Oct 2009
# based on examples from
#[[Complete SQL Database Guide for PySymbian]]# and
#http://www.developer.nokia.com/Community/Wiki/Database_in_PySymbian
# Tested on Nokia N95, S60 3rd edition FP2
 
import e32db,sys,traceback
 
#initiate db access
db=e32db.Dbms()
dbv=e32db.Db_view()
 
# name of the database file
dbname = u'e:\\python\\testdatabase.db'
 
#open database, if it does not exist, then create it
try:
db.open(dbname)
except:
db.create(dbname)
db.open(dbname)
 
 
#Create the database table
try:
# note that the id field is auto-incremented when new rows are added
db.execute(u"CREATE TABLE test_table (id counter, forename VARCHAR, surname VARCHAR)")
print "Table Created"
except SymbianError:
print "Error Creating table: "
#print traceback.format_exception(*sys.exc_info())
print sys.exc_info()[0]
print sys.exc_info()[1]
print sys.exc_info()[2]
 
# attempt to insert data
try:
# note that the id field is NOT required, as it auto-increments
db.execute(u"INSERT INTO test_table (forename, surname) VALUES('joe','bloggs')")
print "Data Inserted"
except SymbianError:
print "Could not insert data: ", SymbianError.args
print sys.exc_info()[0]
print sys.exc_info()[1]
print sys.exc_info()[2]
 
 
#to fetch the whole dataset
try:
dbv.prepare(db,u'SELECT * FROM test_table')
for i in range(1,dbv.count_line()+1): #1 to number of rows
dbv.get_line() #grabs the current row
for i in range(1,dbv.col_count()+1):
print dbv.col(i) #prints each column data
dbv.next_line() #move to the next rowset[[Category:Entertainment]]
except SymbianError:
print "failed to read database: "
print sys.exc_info()[0]
print sys.exc_info()[1]
print sys.exc_info()[2]
 
# close the database
db.close()

Hope this helps someone out.

This page was last modified on 31 May 2013, at 04:05.
64 page views in the last 30 days.