Executing Queries

SQL queries will typically be executed by calling execute() on a query constructed using the query-builder APIs (or by simply iterating over a query object in the case of a Select query). For cases where you wish to execute SQL directly, you can use the Database.execute_sql() method.

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