gpt4 book ai didi

python - 如何使用 python 和 mysql 删除表的内容

转载 作者:行者123 更新时间:2023-11-29 09:30:04 25 4
gpt4 key购买 nike

大家好,我试图在输入新数据之前删除我的表格内容,但我似乎无法让它工作,我不断收到错误,或者有没有更好的方法,然后我尝试这样做,在这里是代码,谢谢大家提前的帮助

import mysql.connector
import pandas as pd
import mysql.connector
from mysql.connector import Error



print ("Clearning Flight Data table for new data")


try:
connection = mysql.connector.connect(host='localhost',
database='flightdata',
user='root',
password='*****')
cursor = connection.cursor()
Delete_all_rows = """flightinfo """
cursor.execute(Delete_all_rows)
connection.commit()
print("All Record Deleted successfully ")

except mysql.connector.Error as error:
print("Failed to Delete all records from database table: {}".format(error))
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")


print ("Running Sydney KWS Domestic update")
import syd_kws_dom


print ("Running Sydney KWS International update")
import syd_kws_int

最佳答案

  • 您可以使用cursor.execute执行delete查询。
Delete_all_rows = """delete from flightinfo """
cursor.execute(Delete_all_rows)

关于python - 如何使用 python 和 mysql 删除表的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58907149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com