aio-libs / aiomysql

aiomysql is a library for accessing a MySQL database from the asyncio
https://aiomysql.rtfd.io
MIT License
1.75k stars 255 forks source link

How does aiomysql handle errors in mysql statements? #419

Open Alex-DataSIM opened 5 years ago

Alex-DataSIM commented 5 years ago

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.

terricain commented 5 years ago

try except?

On Fri, 19 Jul 2019, 11:19 Alex-DataSIM, notifications@github.com 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 .

Vddox commented 5 years ago

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.

terricain commented 5 years ago

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

esundberg commented 4 years ago

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'")
TRGoCPftF commented 3 years ago

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

xiandong79 commented 3 years ago

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