SQLite cursor in Python with statement -
i have following code:
def executeone(self, query, parameters): self.connection cursor: cursor.execute(query, parameters) return cursor.fetchone()
when call method, throws me following error: attributeerror: 'sqlite3.connection' object has no attribute 'fetchone'
doing wrong?
the reason receiving error because connection class not have method called fetchone
. need add .cursor()
create instance of cursor , wrap closing work in with statement.
from contextlib import closing closing(self.connectio.cursor()) cur:
the easiest way deal remove with
statement , manually close cursor
.
cur = self.connection.cursor() try: cur.execute(query, parameters) return cur.fetchone() finally: cur.close()
Comments
Post a Comment