Skip to main content

Module h2o_wave.db

Functions#

connect #

def connect(address:¬†str¬†=¬†None, key_id:¬†str¬†=¬†None, key_secret:¬†str¬†=¬†None) ‚ÄĎ>¬†WaveDBConnection

Returns a connection to a database server.

Args#
address
the address of the database server http(s)://ip:port. Defaults to http://127.0.0.1:10100
key_id
the API access key ID. Defaults to the environment variable H2O_WAVEDB_ACCESS_KEY_ID.
key_secret
the API access key secret. Defaults to the environment variable H2O_WAVEDB_ACCESS_KEY_SECRET.
Returns#

a connection instance

Classes#

WaveDB #

class WaveDB(db: WaveDBConnection, name: str)

Represents a database connector.

Methods#

drop #

async def drop(self) ‚ÄĎ>¬†Optional[str]

Drop the database.

exec #

async def exec(self, sql:¬†str, *params) ‚ÄĎ>¬†Tuple[Optional[List[List]],¬†Optional[str]]

Execute a single SQL statement. Parameters are optional.

Returns a (result, error) tuple, where result is a 2-dimensional list in row-major order, and error is a string error message, if any. The result will be None if the error is not None. Therefore, always check if there is an error before attempting to use the result.

Args#
sql
SQL statement
params
Parameters to the SQL statement, one of str, int, float or None
Returns#

A (result, error) tuple Example:

result, err = db.exec(sql) if err: print(error) return print(result)

result, error = db.exec('CREATE TABLE student(name TEXT, age INTEGER)') result, error = db.exec('INSERT INTO student VALUES ("Alice", 18)') result, error = db.exec('INSERT INTO student VALUES (?, ?)', "Bob", 19) result, error = db.exec('SELECT name, age FROM student WHERE age > 17') result, error = db.exec('SELECT name, age FROM student WHERE age > ?', 17)

exec_atomic #

async def exec_atomic(self, *args) ‚ÄĎ>¬†Tuple[Optional[List[List[List]]],¬†Optional[str]]

Same as exec_may(), but use a transaction. Rollback if any statement fails.

exec_many #

async def exec_many(self, *args) ‚ÄĎ>¬†Tuple[Optional[List[List[List]]],¬†Optional[str]]

Execute multiple SQL statements.

Returns a (results, error) tuple, where results is a list of results from each statement, and error is a string error message, if any. The results will be None if the error is not None. Therefore, always check if there is an error before attempting to use the results.

Args#
args
SQL statements
Returns#

a (results, error) tuple Example:

results, error = db.exec_many( 'CREATE TABLE student(name TEXT, age INTEGER)', 'INSERT INTO student VALUES ("Alice", 18)', ('INSERT INTO student VALUES (?, ?)', "Bob", 19), 'SELECT name, age FROM student WHERE age > 17', ('SELECT name, age FROM student WHERE age > ?', 17), ) if err: print(error) return print(results)

WaveDBConnection #

class WaveDBConnection(address: str = None, key_id: str = None, key_secret: str = None)

Represents a WaveDB database connection.

Create a new database connection.

Methods#

close #

async def close(self)

Close the database connection.

WaveDBError #

class WaveDBError(*args, **kwargs)

Represents a remote exception thrown by the WaveDB database server.

Ancestors#

  • builtins.Exception
  • builtins.BaseException