DBAPI Interface
Note
Supported since PyODPS 0.10.0. As transaction operations are not supported in MaxCompute, related interfaces are not implemented.
PyODPS supports accessing MaxCompute data via Python DBAPI compatible interfaces.
Create connections
Connections can be established via access_id
, access_key
, project` and endpoint
.
>>> import odps.dbapi
>>> conn = odps.dbapi.connect('<access_id>', '<access_key>', '<project>', '<endpoint>')
Existing ODPS entry objects can also be used.
>>> import odps.dbapi
>>> conn = odps.dbapi.connect(o) # type(o) is ODPS
Execute SQL statements
Create a cursor and execute SQL statement on it.
>>> cursor = conn.cursor()
>>> cursor.execute("SELECT * FROM pyodps_iris")
>>> print(cursor.description)
[('sepal_length', 'double', None, None, None, None, True),
('sepal_width', 'double', None, None, None, None, True),
('petal_length', 'double', None, None, None, None, True),
('petal_width', 'double', None, None, None, None, True),
('category', 'string', None, None, None, None, True)]
Read results
Use iterations to read results just like standard DBAPI.
>>> for rec in cursor:
>>> print(rec)
You can also fetch all results at once.
>>> print(cursor.fetchall())