aiomysql icon indicating copy to clipboard operation
aiomysql copied to clipboard

How does aiomysql handle errors in mysql statements?

Open Alex-DataSIM opened this issue 5 years ago • 6 comments

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.

Alex-DataSIM avatar Jul 19 '19 10:07 Alex-DataSIM

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 .

terricain avatar Jul 19 '19 16:07 terricain

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.

Vddox avatar Jul 20 '19 11:07 Vddox

Ah the email missed out your 2nd sentence, I'll have a look around as there are some specific exceptions

terricain avatar Jul 23 '19 11:07 terricain

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'")

esundberg avatar Aug 16 '20 22:08 esundberg

@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.

TRGoCPftF avatar Jul 01 '21 07:07 TRGoCPftF

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())

xiandong79 avatar Jul 20 '21 10:07 xiandong79