DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Simplify Using SQL In Pys60

02.12.2006
| 6137 views |
  • submit to reddit
        I wrote a similar <a href=http://bigbold.com/snippets/posts/show/127>snippet</a> a while ago.
This one add a select_all function and try/except for type 16 (long varbin).
import e32db
db = e32db.Dbms()
dbv = e32db.Db_view()
db.open(u'C:\\system\\data\\bookmarks1.db')

def select_row(query):
    dbv.prepare(db, unicode(query))
    dbv.first_line()
    dbv.get_line()
    result = []
    for i in range(dbv.col_count()):
        try:
            result.append(dbv.col(i+1))
        except:    # in case coltype 16
            result.append(None)
    return result

def select_all(query):
    dbv.prepare(db, unicode(query))
    dbv.first_line()
    rows = []
    for i in range(dbv.count_line()):
        dbv.get_line()
        result = []
        for i in range(dbv.col_count()):
            try:
                result.append(dbv.col(i+1))
            except:    # in case coltype 16
                result.append(None)
        rows.append(result)
        dbv.next_line()
    return rows
Now I can simply call
>>> select_row('SELECT * FROM Favourites')  # 1 row
>>> select_all('SELECT * FROM Favourites')  # all rows
For non-select SQL, you can simply use db.execute(query).