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

时间:2014-03-09 11:53:25

标签: python python-3.x sqlite

我有这个代码,我需要把lastrowid作为一个返回语句。我该如何解决呢?

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)

1 个答案:

答案 0 :(得分:2)

关闭了数据库,因此任何游标都无法再访问数据。在关闭之前检索数据 。我在这里假设你有理由在这里循环重新打开数据库。

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