-1

Using python, how do i get to delete data based on lastrowid. The code i have deletes all the rows CODE:

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")
lobjc
  • 2,751
  • 5
  • 24
  • 30
  • Don't know the API, but it'd make sense that it'd be `cursor.execute("DELETE FROM INPUT33 WHERE rowid=?", m)` Using a real primary key would probably be better than rowid though. – Joachim Isaksson Mar 08 '14 at 07:11
  • Thanks, @Joachim. It has taken me a long time of reading and research to get the print the lastinsertrowid....i recently found that the table name has to be in [], now how do i get the primary key here? regards – lobjc Mar 08 '14 at 07:15

1 Answers1

1

To get the last inserted rowid, use the cursor's lastrowid attribute.

To delete a record with a specific rowid, use that column in the WHERE condition:

cursor.execute("INSERT INTO input33(Name) VALUES(?)", ("whatever",))
rowid = cursor.lastrowid
cursor.execute("DELETE FROM input33 WHERE rowid = ?", (rowid,))
CL.
  • 173,858
  • 17
  • 217
  • 259