The Cursor
class in the Polypheny Python Driver is a fundamental interface for interacting with the database. It allows executing SQL commands, fetching data, and managing the context of a particular database operation. This class follows the Python Database API Specification v2.0 (PEP 249), providing a consistent and familiar interface for database operations.
close()
Closes the cursor. After closing, the cursor cannot be used for executing further commands.
Example:
cur = connection.cursor()
cur.close()
execute(query, params=None, *, fetch_size=None)
Executes an SQL query.
Parameters:
- query (
str
): The SQL query to execute. - params (
list[Any]
, optional): A list of parameters for parameterized queries. - fetch_size (
int
, optional): Number of rows to fetch at a time.
Example:
cur = connection.cursor()
cur.execute("SELECT * FROM users WHERE id = ?", (1,))
print(cur.fetchall())
executemany(query, params)
Executes the given query multiple times with different parameter sets.
Parameters:
- query (
str
): The SQL query to execute. - params (
list[list[Any]]
): A list of lists, where each inner list represents a set of parameters.
Example:
cur = connection.cursor()
cur.executemany("INSERT INTO users (id, name) VALUES (?, ?)", [(1, 'Alice'), (2, 'Bob')])
connection.commit()
executeany(lang, query, params=None, *, fetch_size=None, namespace=None)
Executes a query in any of the supported languages (e.g., SQL, MongoQL).
Parameters:
- lang (
str
): The language in which the query is written (e.g.,sql
,mongo
). - query (
str
): The query string. - params (
list[Any]
, optional): Parameters for the query. - namespace (
str
, optional): Sets the default namespace for the query. - fetch_size (
int
, optional): Number of rows to fetch at a time.
Example:
cur = connection.cursor()
cur.executeany('mongo', 'db.users.find({"id": 1})')
print(cur.fetchone())
fetchone()
Fetches a single row from the result set.
Example:
cur.execute("SELECT * FROM users")
row = cur.fetchone()
print(row)
fetchmany(size=None)
Fetches multiple rows from the result set.
Parameters:
- size (
int
, optional): Number of rows to fetch.
Example:
rows = cur.fetchmany(5)
print(rows)
setinputsizes(sizes)
This method is a no-op (has no effect).
setoutputsize(size, column=None)
This method is a no-op (has no effect).