我希望表最多有三行,因此一旦表有了3行,它就应该删除最旧的一行(rowid1),然后添加新的表。在表还不存在或未达到3行的情况下,它将正常地创建记录。2除了删除第一行之外,一切正常。虽然也没有错误反馈,并且当在DB浏览器中执行命令“execute SQL”时,它可以完美地工作,它只是不工作时,从我的IDE运行。新的记录,但在上面的三个已经存在,而不是被添加为第三后,第一个被删除。
cursor.execute("SELECT count(*) from TableOne")
searchResults = cursor.fetchone()
NoOfRows=searchResults[0]
if NoOfRows ==3:
cursor.execute("DELETE FROM TableOne WHERE rowid=1")
connection.close()
CreateNew()
else:
CreateNew()
请注意,与数据库的连接是在这段代码之前建立的,并且'CreateNew'是一个在表中创建新记录的函数。
Num=1
cursor.execute("DELETE FROM TableOne WHERE rowid=?",[Num])
结果都一样。
1条答案
按热度按时间yqyhoc1h1#
我喜欢@jarh在sqlite3中使用触发器的想法:下面是一个小模型:
输出将如下所示: