# DbUtil

`DbUtil` provides utility methods for interacting with a database, allowing you to query, insert, update, and delete data.

### Methods

#### ~~`queryValue(dbId: Integer, query: String)`~~(Deprecated)

**Description:** Executes a query on the specified database and returns a single string value. It's used for retrieving specific data from a database.

**Parameters:**

* `dbId` (type: Integer): The identifier of the database to query.
* `query` (type: String): The SQL query to execute.

**Returns:** A string containing the result of the query.

**Example: Get the email address of a user based on their username.**

**Use Case**: Useful for retrieving user contact information from a database

{% code fullWidth="false" %}

```typescript
var userEmail = DbUtil.queryValue(2, "SELECT email from demo501db.users where username like 'Samuel'")
```

{% endcode %}

***

#### ~~`insert(dbId: Integer, sql: String)`~~(Deprecated)

**Description:** Inserts data into the specified database using an SQL insert statement and returns the number of affected rows. It's used for adding new data records to a database.

**Parameters:**

* `dbId` (type: Integer): The identifier of the database to insert into.
* `sql` (type: String): The SQL insert statement.

**Returns:** An Integer representing the number of rows affected by the insert operation.

**Example: Insert a new product into the products database.**

**Use Case**: Common in e-commerce systems for adding new items to the product catalog.

```javascript
var newProductInsert = "INSERT INTO products (name, price) VALUES ('New Product', 50)";
var insertedRowCount = DbUtil.insert(2, newProductInsert);
```

***

#### ~~`update(dbId: Integer, sql: String)`~~(Deprecated)

**Description:** Updates data in the specified database using an SQL update statement and returns the number of affected rows. It's used for modifying existing data records in a database.

**Parameters:**

* `dbId` (type: Integer): The identifier of the database to update.
* `sql` (type: String): The SQL update statement.

**Returns:** An Integer representing the number of rows affected by the update operation.

**Example: Update the status of an order in the orders database.**&#x20;

**Use Case:** Common in order management systems to reflect the order's status changes.

```javascript
var updateOrderStatus = "UPDATE orders SET status = 'Shipped' WHERE order_id = 1005";
var updatedRowCount = DbUtil.update(3, updateOrderStatus);
```

***

#### ~~`delete(dbId: Integer, sql: String)`~~(Deprecated)

**Description:** Deletes data from the specified database using an SQL delete statement and returns the number of affected rows. It's used for removing data records from a database.

**Parameters:**

* `dbId` (type: Integer): The identifier of the database to delete from.
* `sql` (type: String): The SQL delete statement.

**Returns:** An Integer representing the number of rows affected by the delete operation.

**Example: Delete expired tasks from the tasks database.**

**Use Case**: Applicable in task management systems to clean up and remove completed or outdated tasks.

```javascript
var deleteExpiredTasks = "DELETE FROM tasks WHERE status = 'Expired'";
var deletedRowCount = DbUtil.delete(4, deleteExpiredTasks);
```

***

#### `listDBConnections()`

**Description:** Lists all database connections..

**Returns:** A list of all database connections as a DBConnection\[].

**Example: Search for database connection by name to get driver.**

```javascript
var dbs = DbUtil.listDBConnections()
for (let i = 0; i < dbs.size(); i++) {
    var db = dbs.get(i)
    if (db.getName().contains("Sample"))
        return db.getDriver().getJdbcDriver()
}
```

***

#### `getDbExecutorByName(dbName: String)`

**Description:** Object that provides database utility methods for querying, inserting, updating, and deleting data.

**Parameters:**

* `dbName` (type: String): The name of the database.

**Returns:** A DbExecutor object.

**Examples and further documentation can be found** [**DbExecutor**](/5.4/scripting/utilities/dbexecutor.md)**.**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.b2winsuite.com/5.4/scripting/utilities/dbutil.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
