Executing Queries
SQL queries will typically be executed by calling execute()
on a queryconstructed using the query-builder APIs (or by simply iterating over a queryobject in the case of a Select
query). For cases where you wish toexecute SQL directly, you can use the Database.execute_sql()
method.
- db = SqliteDatabase('my_app.db')
- db.connect()
- # Example of executing a simple query and ignoring the results.
- db.execute_sql("ATTACH DATABASE ':memory:' AS cache;")
- # Example of iterating over the results of a query using the cursor.
- cursor = db.execute_sql('SELECT * FROM users WHERE status = ?', (ACTIVE,))
- for row in cursor.fetchall():
- # Do something with row, which is a tuple containing column data.
- pass