简体   繁体   中英

Why cursor.execute only executes once and returns in this case?

def SimplifyTrainUser(self):
    self.cursor.execute('SELECT user_id, item_id, look, store, cart, buy FROM user_features limit 0, 50')
    while True:
        str=self.cursor.fetchone()
        if str:
            look_times = len(str['look'].split(',')) if str['look'] else 0
            store_times = len(str['store'].split(',')) if str['store'] else 0
            cart_times = len(str['cart'].split(',')) if str['cart'] else 0
            buy_times = len(str['buy'].split(',')) if str['buy'] else 0
            lru = max(str['look'].split(',') + str['store'].split(',') + str['cart'].split(',') + str['buy'].split(','))
            user_id = str['user_id']
            item_id = str['item_id']

            if not (look_times <=4 and store_times <=1 and cart_times == 0 and buy_times == 0 and int(lru) <= 15 ):
                    self.cursor.execute('INSERT INTO pure_data VALUES(%s,%s,%s,%s,%s,%s)', (user_id, item_id, str['look'],
                                            str['store'], str['cart'], str['buy']))
                    self.db.commit()
                    print "once"
                    global DELETE_INDEX
        else:
            return 0

self.cursor.execute('INSERT INTO pure_data VALUES(%s,%s,%s,%s,%s,%s)', (user_id, item_id, str['look'],str['store'], str['cart'], str['buy'])) is executed only once. If I comment it, "once" can be printed 29 times. What is the reason?

I have found the reason. The cursor is moved every time cursor.execute is called.

When INSERT INTO pure_data , the str=self.cursor.fetchone() equals 0, and the loop ends.

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