aiomysql
aiomysql copied to clipboard
How does aiomysql handle errors in mysql statements?
How does a user of aiomysql handle errors that may be produced while executing sql statements?
There seems to be a lack of documentation regarding error handling on the read the docs site.
try except?
On Fri, 19 Jul 2019, 11:19 Alex-DataSIM, [email protected] wrote:
How does a user of aiomysql handle errors that may be produced while executing sql statements?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/aio-libs/aiomysql/issues/419?email_source=notifications&email_token=AAPE6XNN22ZBY73CYTNKNQTQAGIKNA5CNFSM4IFESNT2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HAHO5LQ, or mute the thread https://github.com/notifications/unsubscribe-auth/AAPE6XMBTMWDC77KA6OBRHTQAGIKNANCNFSM4IFESNTQ .
Sure, I could try except Exception, but that is very broad. I would rather catch specific exceptions. There is no documentation on what coroutines raise what exception.
Ah the email missed out your 2nd sentence, I'll have a look around as there are some specific exceptions
Something like this.
sql = "SELECT * FROM test WHERE broken_here ORDER BY variable ASC"
async with self.conn.cursor() as cursor:
try:
await cursor.execute(sql, params)
result = await cursor.fetchall()
await self.conn.commit()
except Exception as e:
# Catch any Errors
print(f'Error: {e}')
exit()
print(result)
Error: (1054, "Unknown column 'broken_here' in 'where clause'")
@Vddox Old Issue but still open so IDK if it was a miscommunication or has been updated since late 2020.
Just started playing with moving from PyMysql -> Aiomysql and it appears Aiomysql didn't implement any NEW exception classes on top i could tell, but all of the documented PyMysql explicit exception classes are available within Aiomysql
WIKI could use some updating to highlight that info, but it is otherwise available and functional.
yes, not the connection would release or not after an Exception
try:
async with __pool.acquire() as conn:
async with conn.cursor(aiomysql.SSCursor) as cur:
sql_string = "INSERT INTO ....."
await cur.execute(sql_string)
await cur.execute("commit;")
except:
logger.error(traceback.format_exc())