Code:
import e32db,sys,traceback
#initiate db access
db=e32db.Dbms()
dbv=e32db.Db_view()
# name of the database file
dbname = u'c:\\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, name VARCHAR, surname VARCHAR, room_no VARCHAR, block_no VARCHAR,floor_no VARCHAR, wing_no VARCHAR,mobile_no 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(name, surname,room_no,block_no,floor_no,wing_no,mobile_no) VALUES('rose','kunthara','251','0','1','1','123')")
db.execute(u"INSERT INTO test_table(name, surname,room_no,block_no,floor_no,wing_no,mobile_no) VALUES('nayla','pandit','252','0','1','1','234')")
db.execute(u"INSERT INTO test_table(name, surname,room_no,block_no,floor_no,wing_no,mobile_no) VALUES('reetika','malhotra','253','0','1','1','345')")
db.execute(u"INSERT INTO test_table(name, surname,room_no,block_no,floor_no,wing_no,mobile_no) VALUES('sherry','garg','254','0','1','1','456')")
db.execute(u"INSERT INTO test_table(name, surname,room_no,block_no,floor_no,wing_no,mobile_no) VALUES('alekhya','mandadi','255','0','1','1','567')")
db.execute(u"INSERT INTO test_table(name, surname,room_no,block_no,floor_no,wing_no,mobile_no) VALUES('rucha','oza','256','0','1','1','678')")
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()