MySQLdb - Check if row exists Python

后端 未结 3 1071
借酒劲吻你
借酒劲吻你 2021-02-06 18:26

I am trying to check if a row exist with the same Name my database with python and can\'t quite get it here is what I am trying: (I know the connection is wokring)



        
3条回答
  •  余生分开走
    2021-02-06 19:24

    If you want to check for empty results, try if cur.description is None:

    if cursor.description is None:
        # No recordset for INSERT, UPDATE, CREATE, etc
        pass
    else:
        # Recordset for SELECT
    

    As well as:

    exist = cursor.fetchone()
    if exist is None:
      ... # does not exist
    else:
      ... # exists
    

    If you are running a statement that would never return a result set (such as INSERT without RETURNING, or SELECT ... INTO), then you do not need to call .fetchall(); there won't be a result set for such statements. Calling .execute() is enough to run the statement.

提交回复
热议问题