'SQLite3 clear changes() for transaction
I am looking to clear the amount of changes() at the start/end of every transaction.
## self.t = True if MySQL, False if SQLite
## self.cur = (pymysql/sqlite).connection.cursor()
## self._execute(q, o) converts query and options(dict) into:
## -> self.cur.execute(q.format(o)) for MySQL using '{placeholder}' formatting
## -> self.cur.execute(q, o) for SQLite using ':placeholder' formatting
def read(self, q, o):
self.cur.execute("{start} TRANSACTION;".format(start="START" if self.t else "BEGIN"))
self._execute(q, o) ## need to make sure this is not a delete, update or insert statement
results = self.cur.fetchall()
self.cur.execute("SELECT {count};".format(count="ROW_COUNT()" if self.t else "changes()"))
cnt = self.cur.fetchall() #testing
print(cnt) #testing returns [{'changes()':1}]
if(cnt[0]['{count}'.format(count="ROW_COUNT()" if self.t else "changes()")] == -1):
self.cur.execute("COMMIT;")
return results
else:
self.cur.execute("ROLLBACK;")
raise ValueError("Updates are not permitted on read")
While setting up i need add a bunch of data using INSERT however there seems to be no way to clear/reset changes() at the end of the transaction so i can read after.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
