user3346746
user3346746

Reputation: 327

How do I make a return statement from this loop

I have this code and i need to get the lastrowid as a return statement. How can i fix it

def main():   
   while True:      
      #code here
      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() 
         conn.close()      
         for rowid in cursor:break         
         for elem in rowid:
             return rowid#this is not working

            print(m)

Upvotes: 0

Views: 53

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122132

You closed the database, so any cursor no longer has access to the data. Retrieve the data before closing. I am assuming here that you have a reason to re-open the database in a loop here.

def main():   
    while True:      
       for item in name2:
           conn = sqlite3.connect("foods.db")
           cursor = conn.cursor()
           with conn:             
               cursor.execute("INSERT INTO INPUT33 (NAME) VALUES (?);", (name2,))      
               cursor.execute("select MAX(rowid) from [input33];")
               rowid = cursor.fetchone()[0]
           conn.close()
           return rowid

Upvotes: 2

Related Questions