简体   繁体   English

如何从此循环中生成return语句

[英]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. 我有这个代码,我需要把lastrowid作为一个返回语句。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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