### Description keyword examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples showing how to retrieve a table description using a select statement, with support for transactions, aliases, and parameter substitution. ```robotframework ${Person table description}= | Description | select LAST_NAME from person | ``` ```robotframework ${Person table description}= | Description | select LAST_NAME from person | no_transaction=True ``` ```robotframework ${Person table description}= | Description | select LAST_NAME from person | alias=postgres ``` ```robotframework @{parameters} | Create List | person | ``` ```robotframework ${Person table description}= | Description | SELECT * FROM %s | parameters=${parameters} ``` -------------------------------- ### Check If Exists In Database Examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples demonstrating how to use the Check If Exists In Database keyword with various arguments and parameter substitution. ```robotframework Check If Exists In Database | SELECT id FROM person WHERE first_name = 'Franz Allan' | ``` ```robotframework Check If Exists In Database | SELECT id FROM person WHERE first_name = 'John' | msg=my error message ``` ```robotframework Check If Exists In Database | SELECT id FROM person WHERE first_name = 'Franz Allan' | alias=my_alias ``` ```robotframework Check If Exists In Database | SELECT id FROM person WHERE first_name = 'John' | no_transaction=True ``` ```robotframework @{parameters} | Create List | John Check If Exists In Database | SELECT id FROM person WHERE first_name = %s | parameters=${parameters} ``` -------------------------------- ### Install Database Library via pip Source: https://marketsquare.github.io/Robotframework-Database-Library Use this command to install the library. Ensure the specific Python database module for your target database is also installed. ```bash pip install robotframework-databaselibrary ``` -------------------------------- ### Row Count Is Equal To X Examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples demonstrating how to use the Row Count Is Equal To X keyword with various arguments like custom messages, aliases, transaction settings, and parameters. ```robotframework Row Count Is Equal To X | SELECT id FROM person | 1 | ``` ```robotframework Row Count Is Equal To X | SELECT id FROM person | 3 | msg=my error message ``` ```robotframework Row Count Is Equal To X | SELECT id FROM person WHERE first_name = 'John' | 0 | alias=my_alias ``` ```robotframework Row Count Is Equal To X | SELECT id FROM person WHERE first_name = 'John' | 0 | sansTran=True ``` ```robotframework @{parameters} | Create List | John | Row Count Is Equal To X | SELECT id FROM person WHERE first_name = %s | 0 | parameters=${parameters} ``` -------------------------------- ### Set Auto Commit usage examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples showing how to configure the autocommit behavior for database connections, including the use of connection aliases. ```robotframework Set Auto Commit Set Auto Commit | False | Set Auto Commit | True | alias=postgres | ``` -------------------------------- ### Config file with some params only Source: https://marketsquare.github.io/Robotframework-Database-Library Example configuration file that only provides a subset of parameters, here only db_password, under the [default] section. ```ini [default] db_password=mysecret ``` -------------------------------- ### Row Count keyword usage examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples demonstrating the Row Count keyword with standard queries, transaction settings, connection aliases, and parameter substitution. ```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 @{parameters} | Create List | person | ${Rows}= | Row Count | SELECT * FROM %s | parameters=${parameters} ``` -------------------------------- ### Check If Not Exists In Database Examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples demonstrating how to use the Check If Not Exists In Database keyword, which acts as the negation of the existence check. ```robotframework Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'John' | ``` ```robotframework Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'Franz Allan' | msg=my error message ``` ```robotframework Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'Franz Allan' | alias=my_alias ``` ```robotframework Check If Not Exists In Database | SELECT id FROM person WHERE first_name = 'John' | sansTran=True ``` ```robotframework @{parameters} | Create List | John Check If Not Exists In Database | SELECT id FROM person WHERE first_name = %s | parameters=${parameters} ``` -------------------------------- ### Connect to Oracle (oracledb) Source: https://marketsquare.github.io/Robotframework-Database-Library Examples for connecting to Oracle using thin mode, thick mode with default client, and thick mode with a custom library directory. ```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 ``` -------------------------------- ### Oracle: Calling simple IN and OUT parameters procedure Source: https://marketsquare.github.io/Robotframework-Database-Library Robot Framework example for calling the get_second_name procedure. ```robotframework @{params}= Create List Jerry OUTPUT # Second parameter value can be anything, it will be replaced anyway ${param values} ${result sets}= Call Stored Procedure get_second_name ${params} # ${param values} = ['Jerry', 'Schneider'] # ${result sets} = [] ``` -------------------------------- ### Delete All Rows From Table examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples demonstrating how to delete all rows from a table, including options for disabling transactions and specifying database aliases. ```robotframework Delete All Rows From Table | person | ``` ```robotframework Delete All Rows From Table | person | no_transaction=True ``` ```robotframework Delete All Rows From Table | person | alias=my_alias ``` -------------------------------- ### Row Count Is Less Than X usage examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples demonstrating how to use the Row Count Is Less Than X keyword with various arguments like custom error messages, connection aliases, transaction settings, and query parameters. ```robotframework Row Count Is Less Than X | SELECT id FROM person WHERE first_name = 'John' | 1 | ``` ```robotframework Row Count Is Less Than X | SELECT id FROM person WHERE first_name = 'John' | 2 | msg=my error message ``` ```robotframework Row Count Is Less Than X | SELECT id FROM person WHERE first_name = 'John' | 3 | alias=my_alias ``` ```robotframework Row Count Is Less Than X | SELECT id FROM person WHERE first_name = 'John' | 4 | sansTran=True ``` ```robotframework @{parameters} | Create List | John | ``` ```robotframework Row Count Is Less Than X | SELECT id FROM person WHERE first_name = %s | 5 | parameters=${parameters} ``` -------------------------------- ### PostgreSQL Simple IN and OUT Parameters Source: https://marketsquare.github.io/Robotframework-Database-Library Example of a PostgreSQL function with IN and OUT parameters and its corresponding Robot Framework call. ```sql CREATE FUNCTION get_second_name (IN person_first_name VARCHAR(20), OUT person_second_name VARCHAR(20)) LANGUAGE plpgsql AS ' BEGIN SELECT LAST_NAME INTO person_second_name FROM person WHERE FIRST_NAME = person_first_name; END '; ``` ```robotframework @{params}= Create List Jerry ${param values} ${result sets}= Call Stored Procedure get_second_name ${params} # ${param values} = ['Jerry'] # ${result sets} = [[('Schneider',)]] ``` -------------------------------- ### Row Count Is Greater Than X Examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples demonstrating how to use the Row Count Is Greater Than X keyword with various arguments like custom messages, aliases, transaction settings, and parameters. ```robotframework Row Count Is Greater Than X | SELECT id FROM person WHERE first_name = 'John' | 0 | ``` ```robotframework Row Count Is Greater Than X | SELECT id FROM person WHERE first_name = 'John' | 0 | msg=my error message ``` ```robotframework Row Count Is Greater Than X | SELECT id FROM person WHERE first_name = 'John' | 0 | alias=my_alias ``` ```robotframework Row Count Is Greater Than X | SELECT id FROM person | 1 | sansTran=True ``` ```robotframework @{parameters} | Create List | John | Row Count Is Greater Than X | SELECT id FROM person WHERE first_name = %s | 0 | parameters=${parameters} ``` -------------------------------- ### Connect to MySQL (pymysql) Source: https://marketsquare.github.io/Robotframework-Database-Library Examples for connecting to MySQL using default UTF-8 or a custom charset. ```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 ``` -------------------------------- ### Check Query Result Examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples demonstrating various ways to use the Check Query Result keyword, including basic assertions, row/column indexing, type handling, custom error messages, connection aliases, transaction settings, and parameter substitution. ```robotframework Check Query Result | SELECT first_name FROM person | **contains** | Allan | | ``` ```robotframework Check Query Result | SELECT first_name, last_name FROM person | **==** | Schneider | row=1 | col=1 ``` ```robotframework Check Query Result | SELECT id FROM person WHERE first_name = 'John' | **==** | 2 | # Fails, if query returns an integer value | ``` ```robotframework Check Query Result | SELECT id FROM person WHERE first_name = 'John' | **==** | ${2} | # Works, if query returns an integer value | ``` ```robotframework Check Query Result | SELECT first_name FROM person | **equal** | Franz Allan | assertion_message=my error message | ``` ```robotframework Check Query Result | SELECT first_name FROM person | **inequal** | John | alias=my_alias | ``` ```robotframework Check Query Result | SELECT first_name FROM person | **contains** | Allan | no_transaction=True | ``` ```robotframework @{parameters} | Create List | Smith | | | ``` ```robotframework Check Query Result | SELECT first_name FROM person last_name = %s | **contains** | Allan | parameters=${parameters} | ``` -------------------------------- ### Connect to MySQL via ODBC (pyodbc) Source: https://marketsquare.github.io/Robotframework-Database-Library Examples for connecting to MySQL via ODBC, requiring the driver name and optional charset. ```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 ``` -------------------------------- ### Config file with default alias Source: https://marketsquare.github.io/Robotframework-Database-Library Example configuration file using the [default] section, which is equivalent to using no aliases. It includes common connection parameters such as db_module, db_name, db_user, db_password, db_host, and db_port. ```ini [default] db_module=psycopg2 db_name=yourdbname db_user=yourusername db_password=yourpassword db_host=yourhost db_port=yourport ``` -------------------------------- ### Config file with custom DB module specific params Source: https://marketsquare.github.io/Robotframework-Database-Library Example configuration file that includes a custom DB module specific parameter, my_custom_param, under the [default] section. ```ini [default] my_custom_param=value ``` -------------------------------- ### Oracle: Simple IN and OUT parameters procedure definition Source: https://marketsquare.github.io/Robotframework-Database-Library Example SQL procedure definition for a simple get_second_name procedure using IN and OUT parameters. ```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 Microsoft SQL Server (pymssql) Source: https://marketsquare.github.io/Robotframework-Database-Library Examples for connecting to MS SQL Server using default UTF-8 or a custom charset. ```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 ``` -------------------------------- ### Connect to Oracle via JDBC (jaydebeapi) Source: https://marketsquare.github.io/Robotframework-Database-Library Configuration for connecting to Oracle using JDBC, including credential setup, driver path, and optional settings for auto-commit and semicolon handling. ```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 ``` -------------------------------- ### Oracle: Calling procedure returning a result set Source: https://marketsquare.github.io/Robotframework-Database-Library Robot Framework example using the CURSOR value to handle SYS_REFCURSOR output. ```robotframework @{params}= Create List CURSOR # The parameter must have this special value CURSOR ${param values} ${result sets}= Call Stored Procedure get_all_second_names ${params} # ${param values} = [>] # ${result sets} = [[('Franz Allan',), ('Jerry',)], [('See',), ('Schneider',)]] ``` -------------------------------- ### Oracle: Calling procedure returning multiple result sets Source: https://marketsquare.github.io/Robotframework-Database-Library Robot Framework example for calling a procedure with multiple SYS_REFCURSOR OUT parameters. ```robotframework @{params} = Create List CURSOR CURSOR ${param values} ${result sets} = Call Stored Procedure Get_all_first_and_second_names ${params} # ${param values} = [>, >] # ${result sets} = [[('Franz Allan',), ('Jerry',)], [('See',), ('Schneider',)]] ``` -------------------------------- ### Manage Multiple Database Connections with Aliases Source: https://marketsquare.github.io/Robotframework-Database-Library Shows how to connect to multiple databases using aliases and how to switch between them during test execution. ```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 ``` -------------------------------- ### Connect to database using a configuration file Source: https://marketsquare.github.io/Robotframework-Database-Library Connects to a database by providing a path to a configuration file containing connection parameters. ```robotframework Connect To Database | config_file=my_db_params.cfg | | | | | | ``` -------------------------------- ### Row Count Is 0 keyword usage examples Source: https://marketsquare.github.io/Robotframework-Database-Library Examples for the deprecated Row Count Is 0 keyword, including custom error messages, connection aliases, transaction settings, and parameter substitution. ```robotframework Row Count is 0 | SELECT id FROM person WHERE first_name = 'Franz Allan' | ``` ```robotframework Row Count is 0 | SELECT id FROM person WHERE first_name = 'Franz Allan' | msg=my error message ``` ```robotframework Row Count is 0 | SELECT id FROM person WHERE first_name = 'John' | alias=my_alias ``` ```robotframework Row Count is 0 | SELECT id FROM person WHERE first_name = 'John' | sansTran=True ``` ```robotframework @{parameters} | Create List | John Row Count is 0 | SELECT id FROM person WHERE first_name = %s | parameters=${parameters} ``` -------------------------------- ### Basic Database Library Usage Source: https://marketsquare.github.io/Robotframework-Database-Library Demonstrates connecting to an Oracle database and performing basic queries and row count checks. ```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 with basic parameters Source: https://marketsquare.github.io/Robotframework-Database-Library Connects to a database using the psycopg2 module with standard connection details. ```robotframework Connect To Database | psycopg2 | my_db | user | pass | 127.0.0.1 | 5432 | ``` -------------------------------- ### Oracle: Procedure returning a result set definition Source: https://marketsquare.github.io/Robotframework-Database-Library Example SQL procedure definition using SYS_REFCURSOR to return a result set. ```sql get_all_second_names (second_names_cursor OUT SYS_REFCURSOR) AS BEGIN OPEN second_names_cursor for SELECT LAST_NAME FROM person; END; ``` -------------------------------- ### Import DatabaseLibrary without arguments Source: https://marketsquare.github.io/Robotframework-Database-Library The standard way to import the library in a Robot Framework settings section without additional configuration. ```robotframework ***** Settings ***** Library DatabaseLibrary ``` -------------------------------- ### Config file with a specific alias Source: https://marketsquare.github.io/Robotframework-Database-Library Example configuration file using a specific alias section [myoracle]. It includes connection parameters for an Oracle database using the oracledb module. ```ini [myoracle] db_module=oracledb db_name=yourdbname db_user=yourusername db_password=yourpassword db_host=yourhost db_port=yourport ``` -------------------------------- ### Connect to Teradata (teradata) Source: https://marketsquare.github.io/Robotframework-Database-Library Standard connection configuration for Teradata. ```robotframework Connect To Database ... teradata ... db_name=db ... db_user=db_user ... db_password=pass ... db_host=127.0.0.1 ... db_port=1025 ``` -------------------------------- ### Connect to PostgreSQL (psycopg2) Source: https://marketsquare.github.io/Robotframework-Database-Library Standard connection configuration for PostgreSQL. ```robotframework Connect To Database ... psycopg2 ... db_name=db ... db_user=db_user ... db_password=pass ... db_host=127.0.0.1 ... db_port=5432 ``` -------------------------------- ### Connect To Database Using Custom Params (Deprecated) Source: https://marketsquare.github.io/Robotframework-Database-Library Connects to a database using a map string of named arguments. This keyword is deprecated; use the Connect To Database keyword instead. ```robotframework Connect To Database Using Custom Params | psycopg2 | database='my_db_test', user='postgres', password='s3cr3t', host='tiger.foobar.com', port=5432 ``` ```robotframework Connect To Database Using Custom Params | jaydebeapi | 'oracle.jdbc.driver.OracleDriver', 'my_db_test', 'system', 's3cr3t' ``` ```robotframework Connect To Database Using Custom Params | oracledb | user="username", password="pass", dsn="localhost/orclpdb" ``` ```robotframework Connect To Database Using Custom Params | sqlite3 | database="./my_database.db", isolation_level=None ``` -------------------------------- ### Execute Sql Script Source: https://marketsquare.github.io/Robotframework-Database-Library Executes the content of the SQL script file loaded from script_path as SQL commands. SQL commands are expected to be delimited by a semicolon (';') - they will be split and executed separately. Set split to False to disable this behavior. Set external_parser to True to use the external library sqlparse for splitting the script. Set no_transaction to True to run command without explicit transaction commit or rollback in case of error. Use alias to specify what connection should be used if handling multiple database connections. Set replace_robot_variables to resolve RF variables like ${MY_VAR} before executing the SQL. ```APIDOC ## Execute Sql Script ### Description Executes the content of the SQL script file loaded from script_path as SQL commands. SQL commands are expected to be delimited by a semicolon (';') - they will be split and executed separately. Set `split` to _False_ to disable this behavior - in this case the entire script content will be passed to the database module for execution as a single command. Set `external_parser` to _True_ to use the external library sqlparse for splitting the script. Set `no_transaction` to _True_ to run command without explicit transaction commit or rollback in case of error. See Commit behavior for details. Use `alias` to specify what connection should be used if Handling multiple database connections. Set `replace_robot_variables` to resolve RF variables like _${MY_VAR}_ before executing the SQL. ### Method Keyword ### Arguments - **script_path** (str) - Required - Path to the SQL script file. - **no_transaction** (bool) - Optional - Default: False - If True, run command without explicit transaction commit or rollback in case of error. - **alias** (str | None) - Optional - Default: None - The alias of the connection to use. - **split** (bool) - Optional - Default: True - If True, split the script by semicolons and execute separately. - **external_parser** (bool) - Optional - Default: False - If True, use the external library sqlparse for splitting the script. - **replace_robot_variables** (bool) - Optional - Default: False - If True, resolve RF variables like ${MY_VAR} before executing the SQL. - **sqlScriptFileName** (str | None) - Deprecated - Use script_path instead. - **sansTran** (bool | None) - Deprecated - Use no_transaction instead. ### Examples Execute SQL Script | insert_data_in_person_table.sql Execute SQL Script | insert_data_in_person_table.sql | no_transaction=True Execute SQL Script | insert_data_in_person_table.sql | alias=postgres Execute SQL Script | insert_data_in_person_table.sql | split=False ``` -------------------------------- ### Connect to SQLite (sqlite3) Source: https://marketsquare.github.io/Robotframework-Database-Library Connection configuration for SQLite requiring custom parameters. ```robotframework # Using custom parameters required Connect To Database ... sqlite3 ... database=./my_database.db ... isolation_level=${None} ``` -------------------------------- ### Connect to database with custom parameters Source: https://marketsquare.github.io/Robotframework-Database-Library Connects to a database using the psycopg2 module while passing an additional custom parameter. ```robotframework Connect To Database | psycopg2 | my_db | user | pass | 127.0.0.1 | 5432 | my_custom_param=value ``` -------------------------------- ### Connect To Database Using Custom Connection String Source: https://marketsquare.github.io/Robotframework-Database-Library Loads the DB API 2.0 module given db_module and connects to the database using the db_connect_string parsed as a single connection string or URI. Use this keyword 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. ### Arguments - **db_module** (str | None) - Required - The DB API 2.0 module name. - **db_connect_string** (str) - Required - The connection string or URI. - **alias** (str) - Optional - Alias for the connection, default is 'default'. - **dbapiModuleName** (str | None) - Deprecated - Use `db_module` instead. ### Example Usage ``` 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 ``` ``` -------------------------------- ### Connect To Database Source: https://marketsquare.github.io/Robotframework-Database-Library Creates a database connection using the DB API 2.0 db_module and the parameters provided. Supports custom connection parameters, configuration files, and aliases for multiple connections. ```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. ### Arguments - **db_module** (str | None) - Optional - The DB API 2.0 module name. Must be set either as keyword argument or in config file. - **db_name** (str | None) - Optional - Database name. - **db_user** (str | None) - Optional - Database user. - **db_password** (str | None) - Optional - Database password. - **db_host** (str | None) - Optional - Database host. - **db_port** (int | None) - Optional - Database port. If not provided, a commonly used port number for known databases is set as fallback. - **db_charset** (str | None) - Optional - Database charset. If not provided, UTF8 is used as fallback for pymysql, pymssql and pyodbc. - **odbc_driver** (str | None) - Optional - ODBC driver. - **config_file** (str | None) - Optional - Path to configuration file with connection parameters. Defaults to ./resources/db.cfg. - **oracle_driver_mode** (str | None) - Optional - Selects the oracledb client mode. Allowed values: thin (default), thick, thick,lib_dir=. - **alias** (str) - Optional - Alias for the connection. Defaults to 'default'. If the same alias is given twice, the previous connection is overridden. - **custom_connection_params** (**kwargs) - Optional - Additional DB module specific parameters as key/value pairs. ### Usage - All params are optional, although `db_module` must be set - either as keyword argument or in config file. - If some of the listed keyword arguments (db_name, db_host etc.) are not provided (i.e. left on default value None), they are normally not passed to the Python DB module at all, except: - _db_port_ - commonly used port number for known databases is set as fallback - _db_charset_ - _UTF8_ is used as fallback for _pymysql_, _pymssql_ and _pyodbc_ - _oracle_driver_mode_ - _thin_ is used as fallback for _oracledb_ - Other custom params from keyword arguments and config file are passed to the Python DB module as provided - normally as arguments for the _connect()_ function. However, when using **pyodbc** or **ibm_db_dbi**, the connection is established using a **connection string** - so all the custom params are added into it instead of function arguments. - Set `alias` for Handling multiple database connections. If the same alias is given twice, then previous connection will be overridden. - The `oracle_driver_mode` is used to select the **oracledb** client mode. Allowed values are: - _thin_ (default if omitted) - _thick_ - _thick,lib_dir=_ - By default, there is a warning when overwriting an existing connection (i.e. not closing it properly). This can be disabled by setting the `warn_on_connection_overwrite` parameter to **False** in the library import. ### Deprecated Parameters - The old parameters `dbapiModuleName`, `dbName`, `dbUsername`, `dbPassword`, `dbHost`, `dbPort`, `dbCharset`, `dbDriver`, `dbConfigFile` and `driverMode` are **deprecated**, please use new parameters `db_module`, `db_name`, `db_user`, `db_password`, `db_host`, `db_port`, `db_charset`, `odbc_driver`, `config_file` and `oracle_driver_mode` instead. - **The old parameters will be removed in future versions.** ### Basic Examples - Connect To Database | psycopg2 | my_db | user | pass | 127.0.0.1 | 5432 | n- 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 | | | | | | n See Connection examples for different DB modules. ``` -------------------------------- ### Connect To Database Using Custom Params Source: https://marketsquare.github.io/Robotframework-Database-Library Loads the DB API 2.0 module given db_module and connects to the database using the map string db_connect_string parsed as a list of named arguments. This keyword is deprecated; use Connect To Database with custom parameters instead. ```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. ### Arguments - **db_module** (str | None) - Required - The DB API 2.0 module name. - **db_connect_string** (str) - Required - The map string of named arguments. - **alias** (str) - Optional - Alias for the connection, default is 'default'. - **dbapiModuleName** (str | None) - Deprecated - Use `db_module` instead. ### Example Usage ``` 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 ``` ```