I have the following code:
def executeOne(self, query, parameters):
with self.connection as cursor:
cursor.execute(query, parameters)
return cursor.fetchone()
When I call this method, it throws me the following error: AttributeError: 'sqlite3.Connection' object has no attribute 'fetchone'
What am I doing wrong?
The reason you are receiving the error is because the connection class does not have a method called fetchone
. You need add .cursor()
to create an instance of cursor and then wrap it with closing for it to work in a with statement.
from contextlib import closing
with closing(self.connectio.cursor()) as cur:
The easiest way to deal with this is to remove the with
statement and manually close the cursor
.
cur = self.connection.cursor()
try:
cur.execute(query, parameters)
return cur.fetchone()
finally:
cur.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With