Archived:How to make an interactive SQL shell using PySymbian
Aquivado: 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.
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
To learn more easily the default SQL database you can make a small script to act as an interactive SQL shell in the command line interpreter !
First create the test db file with db.create(...). Then type in these lines below in the command line interpreter.
>>> import e32db
>>> db = e32db.Dbms()
>>> dbv = e32db.Db_view()
>>> db.open(u'C:\\test.db')
Here's the query simplification code.
def Q(sql):
if sql.upper().startswith('SELECT'):
dbv.prepare(db, unicode(sql))
dbv.first_line()
rows = []
maxlen = [0] * dbv.col_count()
for i in range(dbv.count_line()):
dbv.get_line()
result = []
for i in range(dbv.col_count()):
try:
val = dbv.col(i+1)
except: # in case coltype 16
val = None
result.append(val)
maxlen[i] = max(maxlen[i], len(str(val)))
rows.append(result)
dbv.next_line()
fmt = '|'+ '|'.join(['%%%ds' % n for n in maxlen]) + '|'
for row in rows:
print fmt % tuple(row)
else:
n = db.execute(unicode(sql))
print '%d rows affected' % n
After that, playing with SQL is quite simple.
>>> Q("CREATE TABLE phone (id COUNTER, name VARCHAR, edition VARCHAR )")
0 rows affected
>>> Q("INSERT INTO phone (name,edition) VALUES ('N6600','2nd')")
1 rows affected
>>> Q("INSERT INTO phone (name,edition) VALUES ('N93','3rd')")
1 rows affected
>>> Q("SELECT * from phone")
|0| 6600| 2nd|
|1| N93| 3rd|


(no comments yet)