2016-01-08 47 views
1

我的當前代碼基本上遍歷所有在我的Excel文件中的行迭代後插入。我想介紹一種可以每隔50行執行一次插入操作的開關斷路器。如何引入一個開關斷路器來執行插入每50行

db = Database(settings) 
     elt_insert_line = "INSERT INTO elt_data VALUES" 
     for row in r: 
      elt_insert_line = elt_insert_line + "(" + row[2] + ", " + row[3] + ")," 
     db.execute(elt_insert_line.rstrip(",")).commit().cleanup() 

回答

1

使用模運算符和IF條件

不熟悉Python,但我認爲你需要像這樣

db = Database(settings) 
     elt_insert_line = "INSERT INTO elt_data VALUES" 
     for row in r: 
      elt_insert_line = elt_insert_line + "(" + row[2] + ", " + row[3] + ")," 

      if r % 50 = 0 then 
      (
       db.execute(elt_insert_line.rstrip(",")).commit().cleanup() 
       elt_insert_line = "INSERT INTO elt_data VALUES" 
      ) 

     --one aditional at the end of the for 
     db.execute(elt_insert_line.rstrip(",")).commit().cleanup()