Python API¶
Database¶
-
class
RPA.Database.
Configuration
¶ Bases:
object
Class to handle configuration from config files and class init
-
all_but_empty
()¶
-
get
(param, default=None)¶
-
get_connection_parameters_as_string
(conf=None)¶
-
parse_arguments
(module_name, database, username, password, host, port, charset, config_file: str)¶
-
set_default_port
(port)¶
-
set_val
(param, value)¶
-
-
class
RPA.Database.
Database
¶ Bases:
object
Database is a library for handling different database operations.
All database operations are supported. Keywords Query and Get Rows return values by default in RPA.Table format.
Library is compatible with any Database API Specification 2.0 module.
References:
Database API Specification 2.0 - http://www.python.org/dev/peps/pep-0249/
Lists of DB API 2.0 - http://wiki.python.org/moin/DatabaseInterfaces
Python Database Programming - http://wiki.python.org/moin/DatabaseProgramming/
Examples
Robot Framework
*** Settings *** Library RPA.Database *** Tasks *** Get Orders From Database Connect To Database pymysql tester user password 127.0.0.1 @{orders} Query Select * FROM incoming_orders FOR ${order} IN @{orders} Handle Order ${order} END
Python
from RPA.Database import Database from RPA.Robocorp.Vault import FileSecrets filesecrets = FileSecrets("secrets.json") secrets = filesecrets.get_secret("databasesecrets") db = Database() db.connect_to_database('pymysql', secrets["DATABASE"], secrets["USERNAME"], secrets["PASSWORD"], '127.0.0.1' ) orders = db.query("SELECT * FROM incoming_orders") for order in orders: print(order)
-
ROBOT_LIBRARY_DOC_FORMAT
= 'REST'¶
-
ROBOT_LIBRARY_SCOPE
= 'GLOBAL'¶
-
call_stored_procedure
(name, params=None, sanstran=False)¶ Call stored procedure with name and params.
- Parameters
name – procedure name
params – parameters for the procedure as a list, defaults to None
sanstran – run command without an explicit transaction commit or rollback, defaults to False
Example:
@{params} Create List FirstParam SecondParam ThirdParam @{results} Call Stored Procedure mystpr ${params}
-
connect_to_database
(module_name: str = None, database: str = None, username: str = None, password: str = None, host: str = None, port: int = None, charset: str = None, config_file: str = 'db.cfg', autocommit: bool = False)¶ Connect to database using DB API 2.0 module.
- Parameters
module_name – database module to use
database – name of the database
username – of the user accessing the database
password – of the user accessing the database
host – SQL server address
port – SQL server port
charset – for example, “utf-8”, defaults to None
config_file – location of configuration file, defaults to “db.cfg”
autocommit – set autocommit value for connect (only with pymssql atm)
Example:
Connect To Database pymysql database username password host port Connect To Database ${CURDIR}${/}resources${/}dbconfig.cfg
-
description
(table)¶ Get description of the SQL table
- Parameters
table – name of the SQL table
Example:
Connect To Database pymysql mydb user pass 127.0.0.1 ${db_description} Description mytable
-
disconnect_from_database
()¶ Close connection to SQL database
Example:
Connect To Database pymysql mydb user pass 127.0.0.1 ${result} Query Select firstname, lastname FROM table Disconnect From Database
-
execute_sql_script
(filename, sanstran=False, encoding='utf-8')¶ Execute content of SQL script as SQL commands.
- Parameters
filename – filepath to SQL script to execute
sanstran – run command without an explicit transaction commit or rollback, defaults to False
encoding – character encoding of file
Example:
Execute SQL Script script.sql
-
get_number_of_rows
(table, conditions=None)¶ Get number of rows in a table. Conditions can be given as arguments for WHERE clause.
- Parameters
table – name of the SQL table
conditions – restrictions for selections, defaults to None
Example:
${count} Get Number Of Rows tablename ${count} Get Number Of Rows tablename column1=5 and column2='x'
-
get_rows
(table, columns=None, conditions=None, as_table=True)¶ Get rows from table. Columns and conditions can be set to filter result.
- Parameters
table – name of the SQL table
columns – name of columns to return, defaults to None means that all columns are returned
conditions – limiting result by WHERE clause, defaults to None
as_table – if result should be instance of
Table
, defaults to True False means that return type would be list
Example:
@{res} Get Rows tablename arvo @{res} Get Rows tablename arvo columns=id,name @{res} Get Rows tablename columns=id conditions=column1='newvalue' @{res} Get Rows tablename conditions=column2='updatedvalue'
-
query
(statement: str, assertion: str = None, sanstran: bool = False, as_table: bool = True)¶ Make a SQL query.
- Parameters
statement – SQL statement to execute
assertion – assert on query result, row_count or columns. Works only for SELECT statements Defaults to None.
sanstran – run command without an explicit transaction commit or rollback, defaults to False
as_table – if result should be instance of
Table
, defaults to True False means that return type would be list
Example:
@{res} Query Select firstname, lastname FROM table FOR ${row} IN @{RES} Log ${row} END @{res} Query Select * FROM table row_count > ${EXPECTED} @{res} Query Select * FROM table 'arvo' in columns @{res} Query Select * FROM table columns == ['id', 'arvo']
-
set_auto_commit
(autocommit=True)¶ Set database auto commit mode.
- Parameters
autocommit – boolean value for auto commit, defaults to True
Example:
Set Auto Commit # auto commit is set on Set Auto Commit False # auto commit is turned off