mysql - 数据库插入频繁导致数据丢失
天蓬老师
天蓬老师 2017-04-17 15:14:37
0
4
1675

插入语句有两条,循环插入这两条
只是简单写了下插入语句,没有捕捉到异常

    def process_item(self, item, spider):
        #print(item)
        try:
            with self.connection.cursor() as cursor:
                #Create a new record
                sql1 = "INSERT INTO staff (XNXQ, \
                                          department, \
                                          teacher, \
                                          gender, \
                                          title, \
                                          note1, \
                                          note2) VALUES (%s, %s, %s, %s, %s, %s, %s)"
                cursor.execute(sql1, (item['first']['XNXQ'],
                                     item['first']['department'],
                                     item['first']['teacher'],
                                     item['first']['gender'],
                                     item['first']['title'],
                                     item['first']['note1'],
                                     item['first']['note2']))
                self.connection.commit()

                #Create a new record
                cursor.execute("select max(id) from staff")
                teacherId = cursor.fetchone()['max(id)']
                print('teacherId:' + str(teacherId))
                print(item['second'])
                    
                sql2 = "INSERT INTO staffCourse (teacherId, \
                                                 snum, \
                                                 course, \
                                                 credit, \
                                                 teachWay, \
                                                 courseType, \
                                                 classNum, \
                                                 className, \
                                                 stuNum, \
                                                 week, \
                                                 section, \
                                                 location) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
                cursor.execute(sql2, (teacherId,
                                      item['second']['snum'],
                                      item['second']['course'],
                                      item['second']['credit'],
                                      item['second']['teachWay'],
                                      item['second']['courseType'],
                                      item['second']['classNum'],
                                      item['second']['className'],
                                      item['second']['stuNum'],
                                      item['second']['week'],
                                      item['second']['section'],
                                      item['second']['location']))
                self.connection.commit()

        except Exception as e:
            print('------------------------------------------')
            print(e)

查看数据库时,发现少了很多,我猜应该是频繁插入导致数据丢失的,因为我在插入数据库之前把数据print了一下,没少。
怎么解决这个问题?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回覆(4)
Peter_Zhu

你是不是一次性循環了很多次啊
如果我沒記錯的話。資料庫有個佇列快取的,如果一下子塞入太多資料佔滿了緩存,就會產生遺失的現象
如果有大量資料要插入的話,就要自己實作佇列,然後定時插入

或試試事務

小葫芦

由於看不懂python語法,僅從sql的角度來提供2種解決方法:
1、用事務的方式去進行寫入數據,每1000條數據提交一次,例如:

fake code

for data.size
    BEGIN
        for 1000
            INSERT INTO ...
        end
    COMMIT
end

2、將sql改成批次寫入,效能有不少提升

INSERT INTO 
(...)
VALUES 
(...),
(...),
(...),
(...);
小葫芦

可以看下資料庫日誌,看下執行記錄。

左手右手慢动作

你雖然程式碼裡面寫了insert之後,commit。但是在什麼時候提交,是在你的專案中的事務中控制的,而不是你在這裡控制的,專案中可能從切面做了事務的控制。解決方案:
1.分頁插,設定事務,不要一次插入,分批插入,分批commit資料。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!