这是我的代码:
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)后删除它。我该怎么办?谢谢!
答案 0 :(得分:0)
首先,您需要使用order by
定义记录的顺序。然后,您可以使用limit 1
Delete from tbstatus
order by id
limit 1
答案 1 :(得分:0)
尝试使用限制而不是ID
curs.execute("""DELETE FROM tbstatus order by id LIMIT 1""")