Mercurial > hg > schema
changeset 1:5baa23a8d32f default tip
more stuff
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Fri, 17 Feb 2012 12:59:09 -0800 |
parents | f7edadebb1de |
children | |
files | schema/sql.py |
diffstat | 1 files changed, 21 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/schema/sql.py Fri Feb 17 12:20:51 2012 -0800 +++ b/schema/sql.py Fri Feb 17 12:59:09 2012 -0800 @@ -33,10 +33,13 @@ def update(self, table, **where): pass - def select(self, table, id, **where): - if id is None: - id = '*' - return self("SELECT ? FROM ? WHERE %s" % 'AND '.join(['%s=%s' % (i, repr(j)) for i, j in where.items()])) + def select(self, table, what=None, **where): + if what is None: + what = '*' + statement = "SELECT %s FROM %s" % (what, table) + if where: + statement += " WHERE %s" % 'AND '.join(['%s=%s' % (i, repr(j)) for i, j in where.items()]) + return self(statement) def tables(self): """return the tables available in the db""" @@ -65,6 +68,14 @@ info = self("PRAGMA table_info(%s)" % table) return [i[1] for i in info] + def insert(self, table, values): + """ + insert a row + """ + assert len(values) == len(self.columns(table)) + self("INSERT INTO %s VALUES (%s)" % (table, ','.join(["?" for i in values])), + values) + if __name__ == '__main__': db = SQL() @@ -77,3 +88,9 @@ assert db.tables() == set(['foo', 'fleem']) columns = db.columns('foo') assert columns == ['bar', 'blarg'] + + # add some values + db.insert('foo', (1, 'BLARG!!!!!')) + db.insert('foo', (7, 'BLARG?????')) + values = db.select('foo') + assert values == [(1, u'BLARG!!!!!'), (7, u'BLARG?????')]