Runs a query with the ``select_statement`` and returns the result as list of rows. The type of row values depends on the database module - usually they are tuples or tuple-like objects. Set ``no_transaction`` to _True_ to run command without explicit transaction com
(
self,
select_statement: str,
no_transaction: bool = False,
return_dict: bool = False,
alias: Optional[str] = None,
parameters: Optional[Tuple] = None,
*,
replace_robot_variables=False,
selectStatement: Optional[str] = None,
sansTran: Optional[bool] = None,
returnAsDict: Optional[bool] = None,
)
| 40 | mapping={"selectStatement": "select_statement", "sansTran": "no_transaction", "returnAsDict": "return_dict"} |
| 41 | ) |
| 42 | def query( |
| 43 | self, |
| 44 | select_statement: str, |
| 45 | no_transaction: bool = False, |
| 46 | return_dict: bool = False, |
| 47 | alias: Optional[str] = None, |
| 48 | parameters: Optional[Tuple] = None, |
| 49 | *, |
| 50 | replace_robot_variables=False, |
| 51 | selectStatement: Optional[str] = None, |
| 52 | sansTran: Optional[bool] = None, |
| 53 | returnAsDict: Optional[bool] = None, |
| 54 | ): |
| 55 | """ |
| 56 | Runs a query with the ``select_statement`` and returns the result as list of rows. |
| 57 | The type of row values depends on the database module - |
| 58 | usually they are tuples or tuple-like objects. |
| 59 | |
| 60 | Set ``no_transaction`` to _True_ to run command without explicit transaction commit or rollback in case of error. |
| 61 | See `Commit behavior` for details. |
| 62 | |
| 63 | Set ``return_dict`` to _True_ to explicitly convert the return values into list of dictionaries. |
| 64 | |
| 65 | Use ``alias`` to specify what connection should be used if `Handling multiple database connections`. |
| 66 | |
| 67 | Use ``parameters`` for query variable substitution (variable substitution syntax may be different |
| 68 | depending on the database client). |
| 69 | |
| 70 | Set ``replace_robot_variables`` to resolve RF variables like _${MY_VAR}_ before executing the SQL. |
| 71 | |
| 72 | === Some parameters were renamed in version 2.0 === |
| 73 | The old parameters ``selectStatement``, ``sansTran`` and ``returnAsDict`` are *deprecated*, |
| 74 | please use new parameters ``select_statement``, ``no_transaction`` and ``return_dict`` instead. |
| 75 | |
| 76 | *The old parameters will be removed in future versions.* |
| 77 | |
| 78 | === Examples === |
| 79 | | ${Results}= | Query | select LAST_NAME from person | |
| 80 | | ${Results}= | Query | select LAST_NAME from person | no_transaction=True | |
| 81 | | ${Results}= | Query | select LAST_NAME from person | return_dict=True | |
| 82 | | ${Results}= | Query | select LAST_NAME from person | alias=postgres | |
| 83 | | @{parameters} | Create List | person | |
| 84 | | ${Results}= | Query | SELECT * FROM %s | parameters=${parameters} | |
| 85 | """ |
| 86 | db_connection = self.connection_store.get_connection(alias) |
| 87 | cur = None |
| 88 | try: |
| 89 | cur = db_connection.client.cursor() |
| 90 | self._execute_sql( |
| 91 | cur, |
| 92 | select_statement, |
| 93 | parameters=parameters, |
| 94 | omit_trailing_semicolon=db_connection.omit_trailing_semicolon, |
| 95 | replace_robot_variables=replace_robot_variables, |
| 96 | ) |
| 97 | all_rows = cur.fetchall() |
| 98 | if all_rows is None: |
| 99 | all_rows = [] |
no test coverage detected