如何删除基于rowid的数据

时间:2014-03-08 07:04:18

标签: python-3.x sqlite last-insert-id rowid

使用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")

1 个答案:

答案 0 :(得分:1)

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

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

cursor.execute("INSERT INTO input33(Name) VALUES(?)", ("whatever",))
rowid = cursor.lastrowid
cursor.execute("DELETE FROM input33 WHERE rowid = ?", (rowid,))