web.py 2.0 documentation

db

Database API (part of web.py)


Functions

f aparam() ...

Returns the appropriate string to be used to interpolate a value with the current web.ctx.db_module or simply %s if there isn't one.

>>> aparam()
'%s'

f reparam(string_, dictionary) ...

Takes a string and a dictionary and interpolates the string using values from the dictionary. Returns an SQLQuery for the result.

>>> reparam("s = $s", dict(s=True))
<sql: "s = 't'">

f sqlquote(a) ...

Ensures a is quoted properly for use in a SQL query.

>>> 'WHERE x = ' + sqlquote(True) + ' AND y = ' + sqlquote(3)
<sql: "WHERE x = 't' AND y = 3">

f connect(dbn, **keywords) ...

Connects to the specified database.

dbn currently must be "postgres", "mysql", or "sqllite".

If DBUtils is installed, connection pooling will be used.

f transact() ...

Start a transaction.

f commit() ...

Commits a transaction.

f rollback() ...

Rolls back a transaction.

f query(sql_query, vars=None, processed=False, _test=False) ...

Execute SQL query sql_query using dictionary vars to interpolate it. If processed=True, vars is a reparam-style list to use instead of interpolating.

>>> query("SELECT * FROM foo", _test=True)
<sql: 'SELECT * FROM foo'>
>>> query("SELECT * FROM foo WHERE x = $x", vars=dict(x='f'), _test=True)
<sql: "SELECT * FROM foo WHERE x = 'f'">
>>> query("SELECT * FROM foo WHERE x = " + sqlquote('f'), _test=True)
<sql: "SELECT * FROM foo WHERE x = 'f'">

f sqllist(lst) ...

Converts the arguments for use in something like a WHERE clause.

>>> sqllist(['a', 'b'])
'a, b'
>>> sqllist('a')
'a'

f sqlors(left, lst) ...

left is a SQL clause liketablename.arg = ` and lst is a list of values. Returns a reparam-style pair featuring the SQL that ORs together the clause for each item in the lst.

>>> sqlors('foo = ', [])
<sql: '2+2=5'>
>>> sqlors('foo = ', [1])
<sql: 'foo = 1'>
>>> sqlors('foo = ', 1)
<sql: 'foo = 1'>
>>> sqlors('foo = ', [1,2,3])
<sql: '(foo = 1 OR foo = 2 OR foo = 3)'>

f select(tables, vars=None, what='*', where=None, order=None, group=None, limit=None, offset=None, _test=False) ...

Selects what from tables with clauses where, order, group, limit, and offset. Uses vars to interpolate. Otherwise, each clause can be a SQLQuery.

>>> select('foo', _test=True)
<sql: 'SELECT * FROM foo'>
>>> select(['foo', 'bar'], where="foo.bar_id = bar.id", limit=5, _test=True)
<sql: 'SELECT * FROM foo, bar WHERE foo.bar_id = bar.id LIMIT 5'>

f insert(tablename, seqname=None, _test=False, **values) ...

Inserts values into tablename. Returns current sequence ID. Set seqname to the ID if it's not the default, or to False if there isn't one.

>>> insert('foo', joe='bob', a=2, _test=True)
<sql: "INSERT INTO foo (a, joe) VALUES (2, 'bob')">

f update(tables, where, vars=None, _test=False, **values) ...

Update tables with clause where (interpolated using vars) and setting values.

>>> joe = 'Joseph'
>>> update('foo', where='name = $joe', name='bob', age=5,
...   vars=locals(), _test=True)
<sql: "UPDATE foo SET age = 5, name = 'bob' WHERE name = 'Joseph'">

f delete(table, where, using=None, vars=None, _test=False) ...

Deletes from table with clauses where and using.

>>> name = 'Joe'
>>> delete('foo', where='name = $name', vars=locals(), _test=True)
<sql: "DELETE FROM foo WHERE name = 'Joe'">

Classes

C UnknownParamstyle(...) ...

raised for unsupported db paramstyles

(currently supported: qmark, numeric, format, pyformat)

This class contains 2 members.

C SQLQuery(...) ...

You can pass this sort of thing as a clause in any db function. Otherwise, you can pass a dictionary to the keyword argument vars and the function will call reparam for you.

This class contains 4 members.

C UnknownDB(...) ...

raised for unsupported dbms

This class contains 2 members.

See the source for more information.