-1

使用 python,我如何根据 lastrowid 删除数据。我的代码删除了所有行代码:

import re
import sys
import difflib
import sqlite3

def main():   
   while True:      
      name = input ('Please Type your Question:  ').lower().split()  
      name2 = name[:]
      import sqlite3
      for item in name2:#break                
         conn = sqlite3.connect("foods.db")
         cursor = conn.cursor()           
         cursor.execute("INSERT INTO INPUT33 (NAME) VALUES (?);", (name2,))      
         cursor.execute("select MAX(rowid) from [input33];")
         conn.commit()      
         for rowid in cursor:break         
         for elem in rowid:
            m = elem            
            print(m)
            cursor.execute("DELETE FROM INPUT33 (NAME) WHERE NAME = name")
4

1 回答 1

1

要获取最后插入的 rowid,请使用游标的lastrowid属性。

要删除具有特定 rowid 的记录,请在 WHERE 条件中使用该列:

cursor.execute("INSERT INTO input33(Name) VALUES(?)", ("whatever",))
rowid = cursor.lastrowid
cursor.execute("DELETE FROM input33 WHERE rowid = ?", (rowid,))
于 2014-03-08T08:49:23.967 回答