neuralib.sqlp.connection

class neuralib.sqlp.connection.Connection[source]

A sqlite3 connection wrapper.

If as a context manager that put itself in a global context-aware variable.

__init__(filename=':memory:', *, debug=False)[source]
Parameters:
  • filename (str | Path) – sqlite database filepath. use in-memory database by default.

  • debug (bool) – print statement when executing.

property connection: Connection
list_table()[source]

list table’s name stored in the database.

Returns:

list of table’s name

Return type:

list[str]

table_schema(table)[source]

get the table schema stored in the database.

Parameters:

table (str | type) – table name or type.

Returns:

table schema

Return type:

str

commit()[source]
rollback()[source]
execute(stat, parameter=())[source]

execute a statement.

Parameters:
  • stat (str | SqlStat) – a raw SQL statement or a SqlStat

  • parameter (list[Any] | dict[str, Any]) – statement variable’s value.

Returns:

a cursor.

Return type:

Cursor

execute_batch(stat, parameter)[source]

execute a statement in batch mode.

Parameters:
  • stat (str | SqlStat) – a raw SQL statement or a SqlStat

  • parameter (list) – list of statement variable’s value.

Returns:

a cursor.

Return type:

Cursor

execute_script(stat)[source]

execute SQL script.

Parameters:
  • stat (str | list[str] | list[SqlStat]) – a raw SQL script, or a list of statements/SqlStat.

  • commit – commit script.

Returns:

a cursor.

sqlite_compileoption_get(n)[source]
Parameters:

n

Returns:

sqlite_compileoption_used(n)[source]
Parameters:

n

Returns:

Return type:

bool

export_dataframe(table)[source]

export a table into a DataFrame.

Parameters:

table (str | type[T]) – table name or type.

Returns:

polars DataFrame

Return type:

DataFrame

import_dataframe(table, df, *, policy='REPLACE')[source]

Import a table from a DataFrame.

Parameters:
  • table (str | type[T]) – table name or type.

  • df (DataFrame) – polars DataFrame

  • policy (Literal['ABORT', 'FAIL', 'IGNORE', 'REPLACE', 'ROLLBACK']) – insert policy

export_csv(table, file)[source]

export a table into a csv file.

Parameters:
  • table (str | type[T]) – table name or type.

  • file (str | Path) – csv filepath.

import_csv(table, file)[source]

Import a table from a csv file.

Parameters:
  • table (str | type[T]) – table name or type.

  • file (str | Path) – csv filepath

neuralib.sqlp.connection.get_connection_context()[source]

Get a connection under the current context.

Return type:

Connection | None