gpt4 book ai didi

python - 如何选择和删除数据库的第一行?

转载 作者:搜寻专家 更新时间:2023-10-30 20:47:44 26 4
gpt4 key购买 nike

这是我的代码:

db = MySQLdb.connect("localhost", "root", "raspberry", "cribdb")
curs=db.cursor()

curs.execute ("""INSERT INTO tbstatus values(NOT NULL, 'status')""")

db.commit()

number_of_rows= curs.execute("SELECT * FROM tbstatus")

if (number_of_rows <= 5):

print(number_of_rows)
else:

curs.execute("""SELECT * from tbstatus""")

row1=curs.fetchone()

db.commit()

curs.execute("""Delete from tbstatus where id = 'row1'""")

db.commit()

print("\n Record Deleted successfully ")

没有错误,但删除功能不起作用。我只想在达到最大限制(5)后删除数据库的第一行。我该怎么办?谢谢!

最佳答案

尝试用limit代替ID

curs.execute("""DELETE FROM tbstatus order by id LIMIT 1""")

关于python - 如何选择和删除数据库的第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53077311/

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