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.

  1. db = SqliteDatabase('my_app.db')
  2. db.connect()
  3.  
  4. # Example of executing a simple query and ignoring the results.
  5. db.execute_sql("ATTACH DATABASE ':memory:' AS cache;")
  6.  
  7. # Example of iterating over the results of a query using the cursor.
  8. cursor = db.execute_sql('SELECT * FROM users WHERE status = ?', (ACTIVE,))
  9. for row in cursor.fetchall():
  10. # Do something with row, which is a tuple containing column data.
  11. pass