Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PYTHON/SQL从表中删除数据以管理数据库_Python_Sql - Fatal编程技术网

PYTHON/SQL从表中删除数据以管理数据库

PYTHON/SQL从表中删除数据以管理数据库,python,sql,Python,Sql,我到处都找不到我问题的答案 你能帮我写一下从表中删除数据的delete\u data方法吗。用户将输入员工id以删除此记录 sql\u delete\u data=“从NOA中删除,其中EmployeeId=?” def delete_数据(自身): 尝试: self.get_连接() 如果result.rowcount!=0: 打印(str(result.rowcount)+“受影响的行”) 其他: 打印(“在数据库中找不到记录”) 例外情况除外,如e: 打印(e) 最后: self.conn

我到处都找不到我问题的答案

你能帮我写一下从表中删除数据的
delete\u data
方法吗。用户将输入员工id以删除此记录

sql\u delete\u data=“从NOA中删除,其中EmployeeId=?”
def delete_数据(自身):
尝试:
self.get_连接()
如果result.rowcount!=0:
打印(str(result.rowcount)+“受影响的行”)
其他:
打印(“在数据库中找不到记录”)
例外情况除外,如e:
打印(e)
最后:
self.conn.close()

您可能想要

id_for_deletion = (42,)

def delete_data(self):
    try:
      self.get_connection()
      # Get a cursor from the connection
      cursor = self.conn.cursor()
      # Execute the SQL statement on the cursor
      result = cursor.execute(sql_delete_data, id_for_deletion)
      # Commit (save) the result
      self.conn.commit()

      if result.rowcount != 0:
        print (str(result.rowcount)+ "Row(s) affected.")
      else:
        print ("Record can not find in the database")

    except Exception as e:
      print(e)
      # Rollback changes on error
      self.conn.rollback()
    finally: 
      self.conn.close()

我根本不遵循代码设置。您有一个脱离实体的方法(但不显示类)、全局范围中的一些查询字符串,并且没有
execute()
来实际运行查询