After creating a :class:`~google.cloud.spanner.instance.Instance`, you can interact with individual databases for that instance.
To list of all existing databases for an instance, use its :meth:`~google.cloud.spanner.instance.Instance.list_databases` method:
databases, token = instance.list_databases()
To create a :class:`~google.cloud.spanner.database.Database` object:
database = instance.database(database_id, ddl_statements)
ddl_statementsis a string containing DDL for the new database.
You can also use :meth:`Instance.database` to create a local wrapper for a database that has already been created:
database = instance.database(existing_database_id)
After creating the database object, use its :meth:`~google.cloud.spanner.database.Database.create` method to trigger its creation on the server:
operation = database.create()
Note
Creating an instance triggers a "long-running operation" and returns an :class:`google.cloud.spanner.database.Operation` object. See :ref:`check-on-current-database-operation` for polling to find out if the operation is completed.
After creating the database object, you can apply additional DDL statements via its :meth:`~google.cloud.spanner.database.Database.update_ddl` method:
operation = instance.update_ddl(ddl_statements, operation_id)
ddl_statementsis a string containing DDL to be applied to the database.operation_idis a string ID for the long-running operation.
Note
Update an instance triggers a "long-running operation" and returns a :class:`google.cloud.spanner.database.Operation` object. See :ref:`check-on-current-database-operation` for polling to find out if the operation is completed.
Drop a databse using its :meth:`~google.cloud.spanner.database.Database.drop` method:
database.drop()
The :meth:`~google.cloud.spanner.database.Database.create` and :meth:`~google.cloud.spanner.database.Database.update` methods of instance object trigger long-running operations on the server, and return instances of the :class:`~google.cloud.spanner.database.Operation` class.
You can check if a long-running operation has finished by using its :meth:`~google.cloud.spanner.database.Operation.finished` method:
>>> operation = instance.create()
>>> operation.finished()
True
Note
Once an :class:`~google.cloud.spanner.instance.Operation` object has returned :data:`True` from its :meth:`~google.cloud.spanner.instance.Operation.finished` method, the object should not be re-used. Subsequent calls to :meth:`~google.cloud.spanner.instance.Operation.finished` will result in an :exc`ValueError` being raised.
Next, learn about :doc:`spanner-session-crud-usage`.