- def exec(self, code: str, inputs: tuple[Any, ...] = tuple()) -> Cursor:
- """Add commands to SQL transaction."""
- return self.conn.execute(code, inputs)
-
- def exec_on_vals(self, code: str, inputs: tuple[Any, ...]) -> Cursor:
- """Wrapper around .exec appending adequate " (?, …)" to code."""
- q_marks_from_values = '(' + ','.join(['?'] * len(inputs)) + ')'
- return self.exec(f'{code} {q_marks_from_values}', inputs)
+ def exec(self,
+ code: str,
+ inputs: tuple[Any, ...] = tuple(),
+ build_q_marks: bool = True
+ ) -> Cursor:
+ """Wrapper around sqlite3.Connection.execute, building '?' if inputs"""
+ if len(inputs) > 0:
+ if build_q_marks:
+ q_marks = ('?' if len(inputs) == 1
+ else '(' + ','.join(['?'] * len(inputs)) + ')')
+ return self.conn.execute(f'{code} {q_marks}', inputs)
+ return self.conn.execute(code, inputs)
+ return self.conn.execute(code)