## Database


Represents a MIKE+ model database.


Usage

``` python
Database(
    model_path,
    *,
    auto_open=True,
)
```


## Attributes

| Name | Description |
|----|----|
| [active_model](#active_model) | Get the name of the active model. |
| [active_scenario](#active_scenario) | Active scenario. |
| [active_simulation](#active_simulation) | Get the active simulation of the database. |
| [alternative_groups](#alternative_groups) | Access to MIKE+ alternative groups. |
| [db_path](#db_path) | Get the path to the database file. |
| [is_open](#is_open) | Check if the database is open. |
| [mupp_path](#mupp_path) | Get the path to the MUPP file. |
| [projection_string](#projection_string) | Get the projection string of the database. |
| [scenarios](#scenarios) | Access to MIKE+ scenario management. |
| [srid](#srid) | Get the Spatial Reference ID (SRID) of the database. |
| [tables](#tables) | A collection of tables in the database. |
| [unit_system](#unit_system) | Get the unit system of the database in MIKE+ format. |
| [version](#version) | Get the version of the database. |

------------------------------------------------------------------------


#### active_model


Get the name of the active model.


`active_model: str`


------------------------------------------------------------------------


#### active_scenario


Active scenario.


`active_scenario: Scenario`


##### Notes

This can be set to a [Scenario](scenarios.Scenario.md#mikeplus.scenarios.Scenario) (for example from `scenarios.by_name()`) to activate a different scenario. For more advanced scenario management, use the [scenarios](Database.md#mikeplus.Database.scenarios) property.


##### Raises


`ValueError`  
If the database is not open or its scenarios are not initialized


------------------------------------------------------------------------


#### active_simulation


Get the active simulation of the database.


`active_simulation: str`


------------------------------------------------------------------------


#### alternative_groups


Access to MIKE+ alternative groups.


`alternative_groups: AlternativeGroupCollection`


##### Raises


`ValueError`  
If the database is not open


------------------------------------------------------------------------


#### db_path


Get the path to the database file.


`db_path: Path`


------------------------------------------------------------------------


#### is_open


Check if the database is open.


`is_open: bool`


------------------------------------------------------------------------


#### mupp_path


Get the path to the MUPP file.


`mupp_path: Path | None`


------------------------------------------------------------------------


#### projection_string


Get the projection string of the database.


`projection_string: str`


------------------------------------------------------------------------


#### scenarios


Access to MIKE+ scenario management.


`scenarios: ScenarioCollection`


##### Raises


`ValueError`  
If the database is not open


------------------------------------------------------------------------


#### srid


Get the Spatial Reference ID (SRID) of the database.


`srid: int`


------------------------------------------------------------------------


#### tables


A collection of tables in the database.


`tables: TableCollection`


This property provides access to all tables in the database through a fluent interface that allows for SQL-like operations on tables. It is the primary entry point for working with tables.


------------------------------------------------------------------------


#### unit_system


Get the unit system of the database in MIKE+ format.


`unit_system: str`


------------------------------------------------------------------------


#### version


Get the version of the database.


`version: str`


## Methods

| Name | Description |
|----|----|
| [__enter__()](#__enter__) | Context manager entry. |
| [__exit__()](#__exit__) | Context manager exit. |
| [__init__()](#__init__) | Initialize a new Database. |
| [__repr__()](#__repr__) | Get nice string representation. |
| [begin_transaction()](#begin_transaction) | Begin the data transaction. |
| [close()](#close) | Close the model database. Closing a closed database does nothing. |
| [create()](#create) | Create a new MIKE+ model database. |
| [end_transaction()](#end_transaction) | End the data transaction. |
| [ensure_mupp()](#ensure_mupp) | Get the model's project file (.mupp), writing one if it has none. |
| [import_from_epanet()](#import_from_epanet) | Import a model from an EPANET .inp file. |
| [import_from_swmm()](#import_from_swmm) | Import a model from a SWMM .inp file. |
| [open()](#open) | Open the model database. |
| [run()](#run) | Run a simulation. |

------------------------------------------------------------------------


#### \_\_enter\_\_()


Context manager entry.


Usage

``` python
__enter__()
```


##### Returns


`Database`  
The opened database.


------------------------------------------------------------------------


#### \_\_exit\_\_()


Context manager exit.


Usage

``` python
__exit__(exc_type, exc_val, exc_tb)
```


------------------------------------------------------------------------


#### \_\_init\_\_()


Initialize a new Database.


Usage

``` python
__init__(model_path, *, auto_open=True)
```


##### Parameters


`model_path: str or Path`  
Path to the model database file (e.g. "model.sqlite" or "model.mupp")

`auto_open: bool = ``True`  
If True, immediately open the database connection


##### Raises


`FileNotFoundError`  
If the database file doesn't exist


------------------------------------------------------------------------


#### \_\_repr\_\_()


Get nice string representation.


Usage

``` python
__repr__()
```


##### Returns


`str`  
The database file name.


------------------------------------------------------------------------


#### begin_transaction()


Begin the data transaction.


Usage

``` python
begin_transaction()
```


Using a BEGIN/END transaction significantly improves batch commit performance.


##### Examples

``` python
>>> from mikeplus import Database
>>> db = Database("path/to/model.sqlite")
>>> db.begin_transaction()
>>> commit = True
>>> try:
>>>     db.tables.msm_Node.update({"Diameter": 0.35}).by_muid("Node_1").execute()
>>>     db.tables.msm_Node.update({"Diameter": 0.40}).by_muid("Node_2").execute()
>>>     # ... update more rows
>>> except RuntimeError as e:
>>>     print(f"An error occurred: {e}")
>>>     commit = False
>>> finally:
>>>     db.end_transaction(commit)
```


##### Raises


`ValueError`  
If the database is not open


------------------------------------------------------------------------


#### close()


Close the model database. Closing a closed database does nothing.


Usage

``` python
close()
```


##### Raises


`DatabaseError`  
If MIKE+ fails to close the database


------------------------------------------------------------------------


#### create()


Create a new MIKE+ model database.


Usage

``` python
create(
    model_path,
    *,
    projection_string="",
    srid=-1,
    auto_open=True,
    overwrite=False
)
```


Writes the `.sqlite` database and a `.mupp` project file beside it, so the new model opens in MIKE+.


##### Parameters


`model_path: str or Path`  
Path where the new database will be created, as `.sqlite` or `.mupp`

`projection_string: str = ``""`  
The projection string (WKT) for the database. The SRID is derived from it when MIKE+ recognises the projection.

`srid: int = ``-1`  
The EPSG code of a projected coordinate system, e.g. ETRS89 / UTM zone 32N is 25832. Geographic systems such as 4326 are not supported; use a projected one such as 3857.

`auto_open: bool = ``True`  
If True, immediately open the database connection

`overwrite: (bool, optional(default is False)) = ``False`  
If True, delete an existing database and project file first. Settings MIKE+ stores in the project file, such as map layers, are lost.


##### Returns


`Database`  
A Database object for the newly created database


##### Raises


`FileExistsError`  
If the database or project file already exists (except if overwrite is True)

`ValueError`  
If both [projection_string](Database.md#mikeplus.Database.projection_string) and [srid](Database.md#mikeplus.Database.srid) are given, or [srid](Database.md#mikeplus.Database.srid) is not a projected coordinate system MIKE+ knows

`DatabaseError`  
If MIKE+ fails to create the database


------------------------------------------------------------------------


#### end_transaction()


End the data transaction.


Usage

``` python
end_transaction(commit=True)
```


##### Parameters


`commit: bool = ``True`  
true is to commit the data into database, false is to rollback the commit.


##### Raises


`ValueError`  
If the database is not open


------------------------------------------------------------------------


#### ensure_mupp()


Get the model's project file (.mupp), writing one if it has none.


Usage

``` python
ensure_mupp()
```


A missing project file is written beside the database with the same name, as MIKE+ does when it opens a bare `.sqlite`. It records the database's model type and unit system and refers to the database by a path relative to itself. An existing project file is never changed.


##### Returns


`Path`  
Path to the project file.


##### Raises


`ValueError`  
If the database is not open.

`DatabaseError`  
If MIKE+ fails to write the project file.


##### Examples

``` python
>>> with mp.open("path/to/model.sqlite") as db:
...     db.ensure_mupp()
```

------------------------------------------------------------------------


#### import_from_epanet()


Import a model from an EPANET .inp file.


Usage

``` python
import_from_epanet(file_path)
```


##### Parameters


`file_path: str or Path`  
Path to the EPANET .inp file. If not provided, a file dialog will open.


##### Examples

``` python
>>> with mp.create("path/to/model.sqlite") as db:
...     db.import_from_epanet("path/to/model.inp")
```


##### Raises


`FileNotFoundError`  
If the file doesn't exist

`ValueError`  
If the file is not an .inp file

`DatabaseError`  
If the import fails


------------------------------------------------------------------------


#### import_from_swmm()


Import a model from a SWMM .inp file.


Usage

``` python
import_from_swmm(file_path)
```


##### Parameters


`file_path: str or Path`  
Path to the SWMM .inp file.


##### Examples

``` python
>>> with mp.create("path/to/model.sqlite") as db:
...     db.import_from_swmm("path/to/model.inp")
```


##### Raises


`FileNotFoundError`  
If the file doesn't exist

`ValueError`  
If the file is not an .inp file

`DatabaseError`  
If the import fails


------------------------------------------------------------------------


#### open()


Open the model database.


Usage

``` python
open()
```


##### Returns


`Database`  
self, for method chaining


##### Raises


`DatabaseError`  
If MIKE+ fails to open the database


------------------------------------------------------------------------


#### run()


Run a simulation.


Usage

``` python
run(simulation_muid=None, *, sim_option=None)
```


##### Parameters


`simulation_muid: str = None`  
Simulation MUID. Defaults to the active simulation.

`sim_option: Literal[str] = None`  
Simulation option. Defaults to None. Possible values are:

- "CS_MIKE_1D": Rivers, collection systems, and overland flows.
- "CS_SWMM": SWMM-based collection systems and overland flows.
- "WD_EPANET": Water distribution systems.
- "CS_MIKE_1D_JobList": Generate job list for LTS simulation (.MJL file).


##### Examples

``` python
>>> with mp.open("path/to/model.sqlite") as db:
...     results = db.run()

>>> with mp.open("path/to/model.sqlite") as db:
...     results = db.run("My Simulation")

>>> db = mp.open("path/to/model.sqlite")
>>> results = db.run("My Simulation")
>>> db.close()
```


##### Returns


`list[Path]`  
Paths to the result files the simulation wrote.


##### Raises


`ValueError`  
If `sim_option` is invalid, or not given and the active model does not determine one, or the model has no simulation with MUID `simulation_muid`.

`RuntimeError`  
If the simulation engine fails to start, exits with an error, or doesn't write its result files. The message names the engine's logs.
