为什么cursor.execute只执行一次并在这种情况下返回?

时间:2015-04-07 02:51:40

标签: python mysql

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']))只执行一次。如果我评论它,&#34;一次&#34;可以打印29次。是什么原因?

1 个答案:

答案 0 :(得分:0)

我找到了原因。每次调用cursor.execute时都会移动光标。

INSERT INTO pure_data时,str=self.cursor.fetchone()等于0,循环结束。