简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM