### Install Robot Framework Database Library
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Install the library using pip. Ensure Python and Robot Framework are installed prerequisites.
```bash
pip install robotframework-databaselibrary
```
--------------------------------
### Database Configuration File Examples
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Examples of configuration files for database connections. Supports default alias and specific aliases. Can include custom DB module specific parameters.
```INI
[default]
db_module=psycopg2
db_name=yourdbname
db_user=yourusername
db_password=yourpassword
db_host=yourhost
db_port=yourport
```
```INI
[myoracle]
db_module=oracledb
db_name=yourdbname
db_user=yourusername
db_password=yourpassword
db_host=yourhost
db_port=yourport
```
```INI
[default]
db_password=mysecret
```
```INI
[default]
my_custom_param=value
```
--------------------------------
### MySQL Database Connection Example
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Example for connecting to a MySQL database using the 'pymysql' Python module.
```RobotFramework
Connect To Database
... pymysql
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
```
--------------------------------
### PostgreSQL Database Connection Example
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Example for connecting to a PostgreSQL database using the 'psycopg2' Python module.
```RobotFramework
Connect To Database
... psycopg2
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=5432
```
--------------------------------
### Oracle Database Connection Examples
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Examples for connecting to Oracle databases using 'oracledb', including thin mode, thick mode, and custom Oracle Instant Client locations.
```RobotFramework
# Thin mode is used by default
Connect To Database
... oracledb
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1521
# Thick mode with default location of the Oracle Instant Client
Connect To Database
... oracledb
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1521
... oracle_driver_mode=thick
# Thick mode with custom location of the Oracle Instant Client
Connect To Database
... oracledb
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1521
... oracle_driver_mode=thick,lib_dir=C:/instant_client_23_5
```
--------------------------------
### Inline Assertion Examples
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Examples demonstrating inline assertions using the Assertion Engine. These keywords check query results against specified conditions and expected values.
```robotframework
Check Row Count
SELECT id FROM person
==
2
```
```robotframework
Check Query Result
SELECT first_name FROM person
contains
Allan
```
--------------------------------
### Microsoft SQL Server Connection Example
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Example for connecting to a Microsoft SQL Server database using 'pymssql', including custom charset specification.
```RobotFramework
# UTF-8 charset is used by default
Connect To Database
... pymssql
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1433
# Specifying a custom charset
Connect To Database
... pymssql
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1433
... db_charset=cp1252
```
--------------------------------
### Basic Database Connection and Query Example
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Demonstrates connecting to an Oracle database and executing a query to retrieve and verify data. Requires 'oracledb' Python module.
```robotframework
*** Settings ***
Library DatabaseLibrary
Test Setup Connect To My Oracle DB
*** Keywords ***
Connect To My Oracle DB
Connect To Database
... oracledb
... db_name=db
... db_user=my_user
... db_password=my_pass
... db_host=127.0.0.1
... db_port=1521
*** Test Cases ***
Get All Names
${Rows}= Query select FIRST_NAME, LAST_NAME from person
Should Be Equal ${Rows}[0][0] Franz Allan
Should Be Equal ${Rows}[0][1] See
Should Be Equal ${Rows}[1][0] Jerry
Should Be Equal ${Rows}[1][1] Schneider
Person Table Contains Expected Records
${sql}= Catenate select LAST_NAME from person
Check Query Result ${sql} contains See
Check Query Result ${sql} equals Schneider row=1
Wait Until Table Gets New Record
${sql}= Catenate select LAST_NAME from person
Check Row Count ${sql} > 2 retry_timeout=5s
Person Table Contains No Joe
${sql}= Catenate SELECT id FROM person
... WHERE FIRST_NAME= 'Joe'
Check Row Count ${sql} == 0
```
--------------------------------
### Run IBM Db2 Docker Container
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/test/readme.md
Starts an IBM Db2 Docker container. This command requires accepting the license and sets up the instance, password, and database name. Note that the database may take a few minutes to start.
```bash
docker pull ibmcom/db2
docker run --rm -itd --name mydb2 --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INSTANCE=db_user -e DB2INST1_PASSWORD=pass -e DBNAME=db ibmcom/db2
```
--------------------------------
### Setup Full Mustache Parameters
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Sets up full Mustache parameters, including expression parameters, program parameters, and hash. Handles optional indentation.
```javascript
setupFullMustacheParams:function(e,t,r,n){var o=e.params;return this.pushParams(o),this.opcode("pushProgram",t),this.opcode("pushProgram",r),e.hash?this.accept(e.hash):this.opcode("emptyHash",n),o}
```
--------------------------------
### Database Library Import Options
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Examples of importing the Database Library with different options to control query result logging and the log head limit.
```robotframework
*** Settings ***
# Default behavior - logging of query results is enabled, log head is 50 rows.
Library DatabaseLibrary
```
```robotframework
*** Settings ***
# Logging of query results is disabled, log head is 50 rows (default).
Library DatabaseLibrary log_query_results=False
```
```robotframework
*** Settings ***
# Logging of query results is enabled (default), log head is 10 rows.
Library DatabaseLibrary log_query_results_head=10
```
```robotframework
*** Settings ***
# Logging of query results is enabled (default), log head limit is disabled (log all rows).
Library DatabaseLibrary log_query_results_head=0
```
--------------------------------
### Oracle Database Connection Example
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to an Oracle database using the 'oracledb' module. Thin mode is used by default.
```robotframework
# Thin mode is used by default
Connect To Database
... oracledb
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1521
# Thick mode with default location
```
--------------------------------
### Connect to Oracle (thick mode)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to an Oracle database using thick mode. Ensure Oracle Instant Client is installed.
```robotframework
Connect To Database
... oracledb
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1521
... oracle_driver_mode=thick
```
--------------------------------
### Connect To Database Keyword Examples
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Demonstrates establishing connections to different databases using the 'Connect To Database' keyword. Supports various DB API 2.0 compliant modules and custom parameters. Connections can also be established via a configuration file.
```robotframework
*** Settings ***
Library DatabaseLibrary
*** Keywords ***
Connect To PostgreSQL
Connect To Database
... psycopg2
... db_name=mydb
... db_user=admin
... db_password=s3cr3t
... db_host=localhost
... db_port=5432
Connect To Oracle Thin Mode
Connect To Database
... oracledb
... db_name=orclpdb
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1521
Connect To Oracle Thick Mode With Custom Client
Connect To Database
... oracledb
... db_name=orclpdb
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1521
... oracle_driver_mode=thick,lib_dir=C:/instant_client_23_5
Connect To MySQL With Custom Charset
Connect To Database
... pymysql
... db_name=mydb
... db_user=root
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... db_charset=cp1252
Connect To MSSQL
Connect To Database
... pymssql
... db_name=mydb
... db_user=sa
... db_password=pass
... db_host=127.0.0.1
... db_port=1433
Connect To SQLite
Connect To Database
... sqlite3
... database=./my_database.db
... isolation_level=${None}
Connect To IBM DB2
Connect To Database
... ibm_db_dbi
... db_name=mydb
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=50000
Connect To MySQL Via ODBC
Connect To Database
... pyodbc
... db_name=mydb
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... odbc_driver={MySQL ODBC 9.2 ANSI Driver}
Connect Via Config File
Connect To Database config_file=./resources/db.cfg
```
--------------------------------
### Run Microsoft SQL Server Docker Container and Create Database
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/test/readme.md
Starts a Microsoft SQL Server Docker container and provides commands to log in and create a database. Ensure the SA password matches the one used in the `docker run` command.
```bash
docker run --rm --name mssql -e ACCEPT_EULA=Y -e MSSQL_SA_PASSWORD='MyPass1234!' -p 1433:1433 -d mcr.microsoft.com/mssql/server
--> login and create DB:
- docker exec -it mssql bash
- /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'MyPass1234!'
- CREATE DATABASE db
- go
```
--------------------------------
### Retry Mechanism Examples
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Examples showcasing the retry mechanism for assertion keywords. These snippets demonstrate how to configure retry timeouts and pauses for waiting until an assertion passes.
```robotframework
Check Row Count
SELECT id FROM person
==
2
retry_timeout=10 seconds
```
```robotframework
Check Query Result
SELECT first_name FROM person
contains
Allan
retry_timeout=5s
retry_pause=1s
```
--------------------------------
### Create Teradata Database
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/test/readme.md
SQL command to create a new database in Teradata. This example allocates 60MB for permanent space and 120MB for spool space.
```sql
CREATE DATABASE db
AS PERMANENT = 60e6, -- 60MB
SPOOL = 120e6; -- 120MB
```
--------------------------------
### Select Data from Person Table
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Selects the LAST_NAME from the person table. This is a basic query example.
```robotframework
${Person table description}= Description select LAST_NAME from person
```
--------------------------------
### Connect to IBM DB2 with ibm_db_dbi
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Connects to an IBM DB2 database using the ibm_db_dbi library. Ensure the necessary driver is installed.
```RobotFramework
Connect To Database
... ibm_db_dbi
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=50000
```
--------------------------------
### Connect to MySQL via ODBC (pyodbc)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to a MySQL database via ODBC using pyodbc. The ODBC driver must be installed.
```robotframework
# ODBC driver name is required
# ODBC driver itself has to be installed
Connect To Database
... pyodbc
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... odbc_driver={MySQL ODBC 9.2 ANSI Driver}
# Specifying a custom charset if needed
Connect To Database
... pyodbc
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... odbc_driver={MySQL ODBC 9.2 ANSI Driver}
... db_charset=latin1
```
--------------------------------
### Connect to MySQL via ODBC with pyodbc
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Connects to a MySQL database via ODBC using the pyodbc library. The ODBC driver name is required and the driver must be installed. Custom charset can be specified.
```RobotFramework
# ODBC driver name is required
# ODBC driver itself has to be installed
Connect To Database
... pyodbc
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... odbc_driver={MySQL ODBC 9.2 ANSI Driver}
# Specifying a custom charset if needed
Connect To Database
... pyodbc
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... odbc_driver={MySQL ODBC 9.2 ANSI Driver}
... db_charset=latin1
```
--------------------------------
### Externalize Connection Parameters with INI File
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Connection parameters can be stored in an INI-style configuration file to externalize sensitive information and simplify connection setup. Keyword arguments always take precedence over values defined in the config file. This allows for combining default configurations with specific overrides or using named aliases defined in the file.
```ini
# ./resources/db.cfg - Default connection (no alias)
[default]
db_module=psycopg2
db_name=mydb
db_user=admin
db_password=s3cr3t
db_host=localhost
db_port=5432
# ./resources/db.cfg - Named alias connection
[myoracle]
db_module=oracledb
db_name=orclpdb
db_user=db_user
db_password=pass
db_host=127.0.0.1
db_port=1521
# Partial config — only override the password, provide other params as keyword args
[default]
db_password=s3cr3t
```
```robotframework
*** Test Cases ***
Connect Using Only Config File
Connect To Database config_file=./resources/db.cfg
Connect Using Named Alias From Config
Connect To Database alias=myoracle config_file=./resources/db.cfg
Combine Config File And Keyword Args
# db_password comes from config file; all other params passed as args
Connect To Database
... psycopg2
... db_name=mydb
... db_user=admin
... db_host=localhost
... db_port=5432
... config_file=./resources/db.cfg
```
--------------------------------
### Handling Multiple Database Connections with Aliases
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Demonstrates connecting to multiple databases (PostgreSQL and MySQL) using aliases and switching between them.
```RobotFramework
*** Settings ***
Library DatabaseLibrary
Test Setup Connect To All Databases
Test Teardown Disconnect From All Databases
*** Keywords ***
Connect To All Databases
Connect To Database
... psycopg2
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=5432
... alias=postgres
Connect To Database
... pymysql
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... alias=mysql
*** Test Cases ***
Using Aliases
${names}= Query select LAST_NAME from person alias=postgres
Execute Sql String drop table XYZ alias=mysql
Switching Default Alias
Switch Database postgres
${names}= Query select LAST_NAME from person
Switch Database mysql
Execute Sql String drop table XYZ
```
--------------------------------
### Getting Top Stack Name
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Returns the name of the variable associated with the current top stack slot.
```javascript
topStackName:function(){return"stack"+this.stackSlot}
```
--------------------------------
### Parameter Substitution for Query Variables
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Demonstrates how to use Robot Framework's `Create List` keyword to prepare parameters for SQL query variable substitution. The `parameters` argument in `Check Row Count` then uses these prepared parameters.
```robotframework
@{parameters} Create List
John
```
```robotframework
Check Row Count
SELECT id FROM person WHERE first_name = %s
equals
5
parameters=${parameters}
```
--------------------------------
### Getting Top Stack Item
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Returns the top item from the appropriate stack (inline or compile stack) without removing it.
```javascript
topStack:function(){var e=this.isInline()?this.inlineStack:this.compileStack,t=e
```
--------------------------------
### Connect to Oracle with custom Instant Client path
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to an Oracle database using thick mode and specifies a custom location for the Oracle Instant Client.
```robotframework
Connect To Database
... oracledb
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=1521
... oracle_driver_mode=thick,lib_dir=C:/instant_client_23_5
```
--------------------------------
### Get Context in Handlebars
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Sets the last context for subsequent operations. This is used to manage the current scope within the template.
```javascript
getContext:function(e){this.lastContext=e}
```
--------------------------------
### PostgreSQL Function with IN and OUT Params (No CURSOR)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Example of a PostgreSQL function with IN and OUT parameters, but without using CURSOR types for output. The output parameter is a VARCHAR.
```sql
CREATE FUNCTION
get_second_name (IN person_first_name VARCHAR(20),
OUT person_second_name VARCHAR(20))
LANGUAGE plpgsql
AS
'\nBEGIN
SELECT LAST_NAME INTO person_second_name
FROM person
WHERE FIRST_NAME = person_first_name;
END
';
```
--------------------------------
### Basic Database Connection and Query
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Connect to an Oracle database and perform a basic query. Requires 'oracledb' Python module.
```RobotFramework
*** Settings ***
Library DatabaseLibrary
Test Setup Connect To My Oracle DB
*** Keywords ***
Connect To My Oracle DB
Connect To Database
... oracledb
... db_name=db
... db_user=my_user
... db_password=my_pass
... db_host=127.0.0.1
... db_port=1521
*** Test Cases ***
Get All Names
${Rows}= Query select FIRST_NAME, LAST_NAME from person
Should Be Equal ${Rows}[0][0] Franz Allan
Should Be Equal ${Rows}[0][1] See
Should Be Equal ${Rows}[1][0] Jerry
Should Be Equal ${Rows}[1][1] Schneider
Person Table Contains Expected Records
${sql}= Catenate select LAST_NAME from person
Check Query Result ${sql} contains See
Check Query Result ${sql} equals Schneider row=1
Wait Until Table Gets New Record
${sql}= Catenate select LAST_NAME from person
Check Row Count ${sql} > 2 retry_timeout=5s
Person Table Contains No Joe
${sql}= Catenate SELECT id FROM person
... WHERE FIRST_NAME= 'Joe'
Check Row Count ${sql} == 0
```
--------------------------------
### Connect To Database Using Config File
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to a database using parameters defined in a configuration file. This is useful for managing sensitive information or complex configurations.
```robotframework
Connect To Database
config_file=my_db_params.cfg
```
--------------------------------
### Import Database Library
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Import the library without any arguments. Optional parameters can be used to configure logging and connection warnings.
```robotframework
*** Settings ***
Library DatabaseLibrary
```
--------------------------------
### Connect To Database
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Creates a database connection using the DB API 2.0 `db_module` and the parameters provided. Along with listed commonly used arguments (`db_name`, `db_host` etc.) you can set any other DB module specific parameters as key/value pairs.
```APIDOC
## Connect To Database
### Description
Creates a database connection using the DB API 2.0 `db_module` and the parameters provided. Along with listed commonly used arguments (`db_name`, `db_host` etc.) you can set any other DB module specific parameters as key/value pairs.
### Method
Connect To Database
### Parameters
#### Path Parameters
- **db_module** (str | None) - Optional - The DB API 2.0 module to use for the connection.
- **db_name** (str) - Optional - The name of the database.
- **db_user** (str) - Optional - The username for authentication.
- **db_password** (str) - Optional - The password for authentication.
- **db_host** (str) - Optional - The hostname or IP address of the database server.
- **db_port** (str) - Optional - The port number for the database connection.
- **db_charset** (str) - Optional - The character set to use for the connection.
- **odbc_driver** (str) - Optional - The ODBC driver to use (for ODBC connections).
- **config_file** (str) - Optional - Path to a configuration file.
- **oracle_driver_mode** (str) - Optional - The Oracle driver mode ('thin', 'thick', or 'thick,lib_dir=').
- **alias** (str) - Optional - An alias for the database connection.
- **warn_on_connection_overwrite** (bool) - Optional - Whether to warn when overwriting an existing connection.
### Request Example
```robotframework
Connect To Database psycopg2 my_db user pass 127.0.0.1 5432
Connect To Database psycopg2 my_db user pass 127.0.0.1 5432 my_custom_param=value
Connect To Database psycopg2 my_db user pass 127.0.0.1 5432 alias=my_alias
Connect To Database config_file=my_db_params.cfg
```
### Response
#### Success Response (200)
- **Connection** - A database connection object.
#### Response Example
(No specific response example provided in the source text)
```
--------------------------------
### PathExpression Handling
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Handles PathExpressions by adding depth, getting context, and looking up values on the context or block parameters. Supports strict mode and data lookups.
```javascript
PathExpression:function(e){this.addDepth(e.depth),this.opcode("getContext",e.depth);var t=e.parts[0],r=t0.default.helpers.scopedId(e),n=!e.depth&&!r&&this.blockParamIndex(t);n?this.opcode("lookupBlockParam",n,e.parts):t?e.data?(this.options.data=!0,this.opcode("lookupData",e.depth,e.parts,e.strict)):this.opcode("lookupOnContext",e.parts,e.falsy,e.strict,r):this.opcode("pushContext")}
```
--------------------------------
### Execute SQL Script
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Reads a `.sql` file and executes its statements against the database. Statements are split on semicolons by default. Useful for setup/teardown scripts.
```APIDOC
## Execute SQL Script
Executes a SQL script file against the database.
### Method
`Execute SQL Script`
### Parameters
- `path` (string) - Required - The path to the `.sql` script file.
- `split` (boolean) - Optional - If True, splits the SQL script by semicolons into multiple statements.
- `external_parser` (boolean) - Optional - If True, uses an external parser for SQL statements.
- `alias` (string) - Optional - The alias of the database connection to use.
- `no_transaction` (boolean) - Optional - If True, the statement is executed outside of a transaction.
### Example
```robotframework
Execute SQL Script ./resources/script_files/insert_data_in_person_table.sql
```
```
--------------------------------
### Get Column Metadata
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Runs a SELECT and returns the cursor's `description` — a list of column metadata tuples. Useful for inspecting column names and types.
```robotframework
*** Test Cases ***
Get Column Metadata
${desc}= Description SELECT first_name, last_name FROM person
Log Column 0 name: ${desc}[0][0] # first_name
Log Column 1 name: ${desc}[1][0] # last_name
Inspect Column Types
${desc}= Description SELECT id, created_at FROM orders
Should Be Equal ${desc}[0][0] id
```
--------------------------------
### Create Parameter List
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Creates a list of parameters to be used in subsequent SQL queries. This is a helper step for dynamic query construction.
```robotframework
@{parameters} Create List person
```
--------------------------------
### Oracle Procedure Returning a Result Set
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Example of an Oracle stored procedure designed to return a result set using the SYS_REFCURSOR type. The procedure opens a cursor for a query and returns it.
```sql
get_all_second_names (second_names_cursor OUT SYS_REFCURSOR) AS
BEGIN
OPEN second_names_cursor for
SELECT LAST_NAME FROM person;
END;
```
--------------------------------
### Connect To Database Using Custom Params (jaydebeapi)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to a database using custom parameters and the jaydebeapi module. This keyword is deprecated and will be removed in future versions. Use 'Connect To Database' instead.
```robotframework
Connect To Database Using Custom Params
jaydebeapi
'oracle.jdbc.driver.OracleDriver', 'my_db_test', 'system', 's3cr3t'
```
--------------------------------
### Oracle Procedure with IN and OUT Params (No Result Sets)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Example of creating an Oracle stored procedure with IN and OUT parameters. This procedure retrieves a last name based on a first name. It does not return any result sets.
```sql
CREATE OR REPLACE PROCEDURE
get_second_name (person_first_name IN VARCHAR, person_second_name OUT VARCHAR) AS
BEGIN
SELECT last_name
INTO person_second_name
FROM person
WHERE first_name = person_first_name;
END;
```
--------------------------------
### Connect To Database Using Custom Connection String
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Connects by passing the entire connection string or URI to the DB module's `connect()` function directly. Useful for modules like psycopg2 with PostgreSQL URIs or oracledb Easy Connect strings.
```robotframework
*** Test Cases ***
Connect Using PostgreSQL URI
Connect To Database Using Custom Connection String
... psycopg2
... postgresql://postgres:s3cr3t@tiger.foobar.com:5432/my_db_test
Connect Using Oracle Easy Connect
Connect To Database Using Custom Connection String
... oracledb
... username/pass@localhost:1521/orclpdb
```
--------------------------------
### PostgreSQL Function Returning Result Sets via refcursor
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Example of a PostgreSQL function that returns multiple result sets using the refcursor type. The function opens two cursors and returns them.
```sql
CREATE FUNCTION
get_all_first_and_second_names(result1 refcursor, result2 refcursor)
RETURNS SETOF refcursor
LANGUAGE plpgsql
AS
'\nBEGIN
OPEN result1 FOR SELECT FIRST_NAME FROM person;
RETURN NEXT result1;
OPEN result2 FOR SELECT LAST_NAME FROM person;
RETURN NEXT result2;
END
';
```
--------------------------------
### Run MySQL Docker Container
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/test/readme.md
Launches a MySQL Docker container with specified root password, database, user, and password. It maps the default MySQL port.
```bash
docker run --rm --name mysql -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_DATABASE=db -e MYSQL_USER=db_user -e MYSQL_PASSWORD=pass -p 3306:3306 -d mysql
```
--------------------------------
### Oracle Procedure Returning Multiple Result Sets
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Example of an Oracle stored procedure returning multiple result sets using multiple SYS_REFCURSOR OUT parameters. Each cursor is opened for a different query.
```sql
CREATE OR REPLACE PROCEDURE
get_all_first_and_second_names(first_names_cursor OUT SYS_REFCURSOR, second_names_cursor OUT SYS_REFCURSOR) AS
BEGIN
OPEN first_names_cursor for
SELECT FIRST_NAME FROM person;
OPEN second_names_cursor for
SELECT LAST_NAME FROM person;
END;
```
--------------------------------
### Switch Database
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Switch the default database connection to the specified alias.
```APIDOC
## Switch Database
### Description
Switch the default database connection to the specified alias.
### Method
Switch Database
### Parameters
#### Positional or Named Parameters
- **alias** (str) - Required - The alias of the database connection to switch to.
### Examples
```robotframework
Switch Database my_alias
Switch Database alias=my_alias
```
```
--------------------------------
### Connect to Oracle via JDBC with jaydebeapi
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Connects to an Oracle database via JDBC using jaydebeapi. Username and password must be provided as a dictionary. The JAR file with the Oracle JDBC driver is required. Jaydebeapi is not natively supported, so custom parameters are used.
```RobotFramework
# Username and password must be set as a dictionary
VAR &{CREDENTIALS} user=db_user password=pass
# JAR file with Oracle JDBC driver is required
# Jaydebeapi is not "natively" supported by the Database Library,
# so using the custom parameters
Connect To Database
... jaydebeapi
... jclassname=oracle.jdbc.driver.OracleDriver
... url=jdbc:oracle:thin:@127.0.0.1:1521/db
... driver_args=${CREDENTIALS}
... jars=C:/ojdbc17.jar
# Set if getting error 'Could not commit/rollback with auto-commit enabled'
Set Auto Commit False
# Set for automatically removing trailing ';' (might be helpful for Oracle)
Set Omit Trailing Semicolon True
```
--------------------------------
### Connect To Database Using Custom Params
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
*DEPRECATED* Use new `Connect To Database` keyword with custom parameters instead. The deprecated keyword will be removed in future versions.
```APIDOC
## Connect To Database Using Custom Params
### Description
*DEPRECATED* Use new `Connect To Database` keyword with custom parameters instead. The deprecated keyword will be removed in future versions.
Loads the DB API 2.0 module given `db_module` then uses it to connect to the database using the map string `db_connect_string` (parsed as a list of named arguments).
Use `connect_to_database_using_custom_connection_string` for passing all params in a single connection string or URI.
The old parameter `dbapiModuleName` is deprecated, please use new parameter `db_module` instead.
### Parameters
#### Path Parameters
- **db_module** (str | None) - Optional - The name of the DB API 2.0 module to load. Defaults to None.
- **db_connect_string** (str) - Optional - The database connection string, parsed as a list of named arguments.
- **alias** (str) - Optional - An alias for the database connection. Defaults to 'default'.
- **dbapiModuleName** (str | None) - Optional - Deprecated. Use `db_module` instead. Defaults to None.
### Request Example
```robotframework
Connect To Database Using Custom Params psycopg2 database='my_db_test', user='postgres', password='s3cr3t', host='tiger.foobar.com', port=5432
Connect To Database Using Custom Params jaydebeapi 'oracle.jdbc.driver.OracleDriver', 'my_db_test', 'system', 's3cr3t'
Connect To Database Using Custom Params oracledb user="username", password="pass", dsn="localhost/orclpdb"
Connect To Database Using Custom Params sqlite3 database="./my_database.db", isolation_level=None
```
### Response
#### Success Response (200)
- **None** - This keyword does not return a value upon success.
```
--------------------------------
### Decorator Function Implementation
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
This snippet shows the implementation of a decorator function within a JavaScript context, likely for a templating engine or similar framework. It handles function lookup, argument setup, and decorator registration.
```javascript
Decorator:function(e,t){var r=this.nameLookup("decorators",t,"decorator"),n=this.setupHelperArgs(t,e);this.decorators.push(["fn = ",this.decorators.functionCall(r,"",["fn","props","container",n])," || fn;"])}
```
--------------------------------
### Execute SQL Script File
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Reads a `.sql` file and executes its statements against the database. Statements are split on semicolons by default. Useful for setup/teardown scripts. Supports `split=False` to prevent splitting and `external_parser=True` for advanced parsing.
```robotframework
*** Settings ***
Library DatabaseLibrary
Suite Setup Connect To Database psycopg2 db_name=mydb db_user=u db_password=p db_host=localhost
*** Test Cases ***
Run Setup Script
Execute SQL Script ./resources/script_files/insert_data_in_person_table.sql
Run Script Without Splitting
Execute SQL Script ./resources/script_files/single_block.sql split=False
Run Script With External sqlparse Parser
Execute SQL Script ./resources/script_files/complex_proc.sql external_parser=True
Run Script On Specific Alias
Execute SQL Script ./resources/script_files/insert_data_in_person_table.sql alias=postgres
Run Script Without Transaction
Execute SQL Script ./resources/script_files/insert_data_in_person_table.sql no_transaction=True
```
--------------------------------
### Get Row Count from Database
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Runs a query and returns the number of rows in the result. Supports transaction control, aliasing connections, and variable substitution. Deprecated parameters `selectStatement` and `sansTran` exist for backward compatibility.
```robotframework
${Rows}= Row Count select LAST_NAME from person
```
```robotframework
${Rows}= Row Count select LAST_NAME from person no_transaction=True
```
```robotframework
${Rows}= Row Count select LAST_NAME from person alias=postgres
```
```robotframework
${Rows}= Row Count SELECT * FROM %s parameters=${parameters}
```
--------------------------------
### Connect To Database with Aliases
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Use the `alias` parameter to manage multiple simultaneous database connections. Subsequent keywords can target a specific connection via `alias=`, or the default connection can be changed using `Switch Database`.
```robotframework
*** Settings ***
Library DatabaseLibrary
Test Setup Connect To All Databases
Test Teardown Disconnect From All Databases
*** Keywords ***
Connect To All Databases
Connect To Database
... psycopg2
... db_name=mydb
... db_user=user
... db_password=pass
... db_host=127.0.0.1
... db_port=5432
... alias=postgres
Connect To Database
... pymysql
... db_name=mydb
... db_user=user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... alias=mysql
*** Test Cases ***
Query Different Databases With Aliases
${pg_names}= Query SELECT last_name FROM person alias=postgres
${my_names}= Query SELECT last_name FROM person alias=mysql
Should Be Equal ${pg_names}[0][0] ${my_names}[0][0]
Switch Default Connection And Query
Switch Database postgres
${names}= Query SELECT last_name FROM person
# Queries postgres now by default
Switch Database mysql
ExecuteSql String DROP TABLE IF EXISTS temp_table # Runs on mysql now
```
--------------------------------
### Create List of Parameters for Query
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Creates a list that can be used as parameters for parameterized queries.
```robotframework
@{parameters} Create List person
```
--------------------------------
### Connect To Database
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Connects to a database using specified parameters. Supports aliases for managing multiple connections simultaneously.
```APIDOC
## Connect To Database
### Description
Connects to a database using the provided connection details. This keyword can be used with an `alias` parameter to manage multiple database connections concurrently. Subsequent operations can target a specific connection using its alias, or the default connection can be changed using the `Switch Database` keyword.
### Method
Connect To Database
### Parameters
- **db_driver** (string) - Required - The database driver to use (e.g., `psycopg2`, `pymysql`).
- **db_name** (string) - Optional - The name of the database.
- **db_user** (string) - Optional - The username for authentication.
- **db_password** (string) - Optional - The password for authentication.
- **db_host** (string) - Optional - The hostname or IP address of the database server.
- **db_port** (integer) - Optional - The port number for the database connection.
- **alias** (string) - Optional - An alias to identify this specific connection.
### Request Example
```robotframework
Connect To Database
... psycopg2
... db_name=mydb
... db_user=user
... db_password=pass
... db_host=127.0.0.1
... db_port=5432
... alias=postgres
```
```
--------------------------------
### Connect to SQLite with sqlite3
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/README.md
Connects to an SQLite database using the sqlite3 library. Custom parameters are required, including the database file path and isolation level.
```RobotFramework
# Using custom parameters required
Connect To Database
... sqlite3
... database=./my_database.db
... isolation_level=${None}
```
--------------------------------
### Connect To Database Using Custom Connection String
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Loads a DB API 2.0 module and connects to the database using a provided connection string. Use this when parameters need to be passed as a single string or URI.
```robotframework
Connect To Database Using Custom Connection String
db_module=pyodbc
db_connect_string=DRIVER={ODBC Driver 17 for SQL Server};SERVER=server_name;DATABASE=db_name;UID=user;PWD=password
```
--------------------------------
### Connect To Database Using Custom Params (psycopg2)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to a database using custom parameters and the psycopg2 module. This keyword is deprecated and will be removed in future versions. Use 'Connect To Database' instead.
```robotframework
Connect To Database Using Custom Params
psycopg2
database='my_db_test', user='postgres', password='s3cr3t', host='tiger.foobar.com', port=5432
```
--------------------------------
### Connect To Database Using Custom Connection String
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Loads the DB API 2.0 module given `db_module` then uses it to connect to the database using the `db_connect_string` (parsed as single connection string or URI). Use `Connect To Database` for passing custom connection params as named arguments.
```APIDOC
## Connect To Database Using Custom Connection String
### Description
Loads the DB API 2.0 module given `db_module` then uses it to connect to the database using the `db_connect_string` (parsed as single connection string or URI). Use `Connect To Database` for passing custom connection params as named arguments.
### Method
Connect To Database Using Custom Connection String
### Parameters
#### Path Parameters
- **db_module** (str | None) - Optional - The DB API 2.0 module to use for the connection.
- **db_connect_string** (str) - Optional - The connection string or URI for the database.
- **alias** (str) - Optional - An alias for the database connection. Defaults to 'default'.
- **dbapiModuleName** (str | None) - Optional - Deprecated. Use `db_module` instead.
### Request Example
(No specific request example provided in the source text for this keyword, but it implies usage with `db_connect_string`)
### Response
#### Success Response (200)
- **Connection** - A database connection object.
#### Response Example
(No specific response example provided in the source text)
```
--------------------------------
### Connect To Database Using Custom Params (sqlite3)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to a SQLite database using custom parameters. This keyword is deprecated and will be removed in future versions. Use 'Connect To Database' instead.
```robotframework
Connect To Database Using Custom Params
sqlite3
database="./my_database.db", isolation_level=None
```
--------------------------------
### Description
Source: https://context7.com/marketsquare/robotframework-database-library/llms.txt
Runs a SELECT query and returns the cursor's `description`, which is a list of column metadata tuples. This includes column name, type, size, and nullability.
```APIDOC
## Description
Gets column metadata for a query.
### Method
`Description`
### Parameters
- `sql` (string) - Required - The SQL query to execute.
- `parameters` (list) - Optional - A list of parameters to be used in the SQL query.
### Returns
- `description` (list) - A list of tuples, where each tuple contains column metadata (name, type_code, display_size, internal_size, precision, scale, null_ok).
### Example
```robotframework
${desc}= Description SELECT first_name, last_name FROM person
```
```
--------------------------------
### Connect To Database Using Custom Params (oracledb)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to an Oracle database using custom parameters. This keyword is deprecated and will be removed in future versions. Use 'Connect To Database' instead.
```robotframework
Connect To Database Using Custom Params
oracledb
user="username", password="pass", dsn="localhost/orclpdb"
```
--------------------------------
### Connect to SQLite (sqlite3)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to an SQLite database. Custom parameters like 'database' and 'isolation_level' are required.
```robotframework
# Using custom parameters required
Connect To Database
... sqlite3
... database=./my_database.db
... isolation_level=${None}
```
--------------------------------
### Call Stored Procedure with IN and OUT parameters (MS SQL)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Demonstrates calling a stored procedure with both input and output parameters in MS SQL Server using pymssql. The OUT parameter values must be provided as sample values in `additional_output_params` for type inference.
```robotframework
@{params}= Create List give me 1
@{out_params}= Create List ${9}
${param values} ${result sets}= Call Stored Procedure return_out_param_without_result_sets \
... ${params} additional_output_params=${out_params}
# ${result sets} = []
# ${param values} = ('give me 1', 1)
```
--------------------------------
### Select Data with Parameters
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Selects all columns from a table using a parameterized query. The table name is provided via the 'parameters' argument, allowing for dynamic table selection.
```robotframework
${Person table description}= Description SELECT * FROM %s parameters=${parameters}
```
--------------------------------
### Connect To Database Using Custom Connection String
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Loads the DB API 2.0 module given `db_module` then uses it to connect to the database using the `db_connect_string` (parsed as single connection string or URI).
```APIDOC
## Connect To Database Using Custom Connection String
### Description
Loads the DB API 2.0 module given `db_module` then uses it to connect to the database using the `db_connect_string` (parsed as single connection string or URI).
### Parameters
#### Path Parameters
- **db_module** (str) - Required - The name of the DB API 2.0 module to load (e.g., psycopg2, oracledb).
- **db_connect_string** (str) - Required - The database connection string or URI.
- **alias** (str) - Optional - An alias for the database connection. Defaults to 'default'.
### Request Example
```robotframework
Connect To Database Using Custom Connection String psycopg2 postgresql://postgres:s3cr3t@tiger.foobar.com:5432/my_db_test
Connect To Database Using Custom Connection String oracledb username/pass@localhost:1521/orclpdb
```
### Response
#### Success Response (200)
- **None** - This keyword does not return a value upon success.
```
--------------------------------
### Connect To Database with psycopg2
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to a PostgreSQL database using the psycopg2 module. Supports custom parameters and aliasing.
```robotframework
Connect To Database
psycopg2
my_db
user
pass
127.0.0.1
5432
```
```robotframework
Connect To Database
psycopg2
my_db
user
pass
127.0.0.1
5432
my_custom_param=value
```
```robotframework
Connect To Database
psycopg2
my_db
user
pass
127.0.0.1
5432
alias=my_alias
```
--------------------------------
### Connect to MySQL (pymysql)
Source: https://github.com/marketsquare/robotframework-database-library/blob/master/doc/index.html
Connects to a MySQL database using pymysql. UTF-8 charset is used by default.
```robotframework
# UTF-8 charset is used by default
Connect To Database
... pymysql
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
# Specifying a custom charset
Connect To Database
... pymysql
... db_name=db
... db_user=db_user
... db_password=pass
... db_host=127.0.0.1
... db_port=3306
... db_charset=cp1252
```