Runs a query with the ``select_statement`` and returns the number of rows in the result. 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 wh
(
self,
select_statement: str,
no_transaction: bool = False,
alias: Optional[str] = None,
parameters: Optional[Tuple] = None,
*,
replace_robot_variables=False,
selectStatement: Optional[str] = None,
sansTran: Optional[bool] = None,
)
| 108 | |
| 109 | @renamed_args(mapping={"selectStatement": "select_statement", "sansTran": "no_transaction"}) |
| 110 | def row_count( |
| 111 | self, |
| 112 | select_statement: str, |
| 113 | no_transaction: bool = False, |
| 114 | alias: Optional[str] = None, |
| 115 | parameters: Optional[Tuple] = None, |
| 116 | *, |
| 117 | replace_robot_variables=False, |
| 118 | selectStatement: Optional[str] = None, |
| 119 | sansTran: Optional[bool] = None, |
| 120 | ): |
| 121 | """ |
| 122 | Runs a query with the ``select_statement`` and returns the number of rows in the result. |
| 123 | |
| 124 | Set ``no_transaction`` to _True_ to run command without explicit transaction commit or rollback in case of error. |
| 125 | See `Commit behavior` for details. |
| 126 | |
| 127 | Use ``alias`` to specify what connection should be used if `Handling multiple database connections`. |
| 128 | |
| 129 | Use ``parameters`` for query variable substitution (variable substitution syntax may be different |
| 130 | depending on the database client). |
| 131 | |
| 132 | Set ``replace_robot_variables`` to resolve RF variables like _${MY_VAR}_ before executing the SQL. |
| 133 | |
| 134 | === Some parameters were renamed in version 2.0 === |
| 135 | The old parameters ``selectStatement`` and ``sansTran`` are *deprecated*, |
| 136 | please use new parameters ``select_statement`` and ``no_transaction`` instead. |
| 137 | |
| 138 | *The old parameters will be removed in future versions.* |
| 139 | |
| 140 | === Examples === |
| 141 | | ${Rows}= | Row Count | select LAST_NAME from person | |
| 142 | | ${Rows}= | Row Count | select LAST_NAME from person | no_transaction=True | |
| 143 | | ${Rows}= | Row Count | select LAST_NAME from person | alias=postgres | |
| 144 | | @{parameters} | Create List | person | |
| 145 | | ${Rows}= | Row Count | SELECT * FROM %s | parameters=${parameters} | |
| 146 | """ |
| 147 | db_connection = self.connection_store.get_connection(alias) |
| 148 | cur = None |
| 149 | try: |
| 150 | cur = db_connection.client.cursor() |
| 151 | self._execute_sql( |
| 152 | cur, |
| 153 | select_statement, |
| 154 | parameters=parameters, |
| 155 | omit_trailing_semicolon=db_connection.omit_trailing_semicolon, |
| 156 | replace_robot_variables=replace_robot_variables, |
| 157 | ) |
| 158 | data = cur.fetchall() |
| 159 | if data is None: |
| 160 | data = [] |
| 161 | self._commit_if_needed(db_connection, no_transaction) |
| 162 | col_names = [c[0] for c in cur.description] |
| 163 | if db_connection.module_name in ["sqlite3", "ibm_db", "ibm_db_dbi", "pyodbc", "jaydebeapi"]: |
| 164 | current_row_count = len(data) |
| 165 | else: |
| 166 | current_row_count = cur.rowcount |
| 167 | logger.info(f"Retrieved {current_row_count} rows") |
no test coverage detected