pwtools.sql.makedb

pwtools.sql.makedb(filename, lists, colnames, table=None, mode='a', close=True, **kwds)[source]

Create sqlite db filename (mode=’w’) or append to existing db (mode=’a’). The database is build up from lists and colnames, see sql_matrix().

In append mode, rows are simply added to the bottom of the table and only column names (colnames) which are already in the table are allowed. colnames can contain a subset of the original header, in which case the other entries are NULL by default.

If the datsbase file doesn’t exist, then mode=’a’ is the same as mode=’w’.

By default close=True, i.e. a db with a closed connection is returned. For interactive use, close=False is what you want. That gives you a db which can be used right away.

Parameters:
  • lists (list of lists, see sql_matrix())

  • colnames (list of column names, see sql_matrix())

  • table (str, optional) – String with table name. If None then we try to set a default name based on filename.

  • mode (str) – ‘w’: write new db, ‘a’: append

  • close (bool, optional) – Close cursor after db has been filled with values.

  • **kwds (passed to sql_matrix())

Returns:

db

Return type:

SQLiteDB instance

Examples

>>> lists=zip([1,2,3],['a','b','c'])
>>> db=sql.makedb('/tmp/foo.db', lists, ['col0', 'col1'], mode='w',
...               table='calc', close=False)
>>> db.get_dict('select * from calc')
{'col0': [1, 2, 3], 'col1': [u'a', u'b', u'c']}