我想加載一個CSV文件看起來像這樣:幫助與MySQL LOAD DATA INFILE
Acct. No.,1-15 Days,16-30 Days,31-60 Days,61-90 Days,91-120 Days,Beyond 120 Days
2314134101,898.89,8372.16,5584.23,7744.41,9846.54,2896.25
2414134128,5457.61,7488.26,9594.02,6234.78,273.7,2356.13
2513918869,2059.59,7578.59,9395.51,7159.15,5827.48,3041.62
1687950783,4846.85,8364.22,9892.55,7213.45,8815.33,7603.4
2764856043,5250.11,9946.49,8042.03,6058.64,9194.78,8296.2
2865446086,596.22,7670.04,8564.08,3263.85,9662.46,7027.22
,4725.99,1336.24,9356.03,1572.81,4942.11,6088.94
,8248.47,956.81,8713.06,2589.14,5316.68,1543.67
,538.22,1473.91,3292.09,6843.89,2687.07,9808.05
,9885.85,2730.72,6876,8024.47,1196.87,1655.29
但是,如果你發現,一些字段是不完整的。我想MySQL會跳過第一列丟失的那一行。當我運行命令:
LOAD DATA LOCAL INFILE 'test-long.csv' REPLACE INTO TABLE accounts
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(cf_535, cf_580, cf_568, cf_569, cf_571, cf_572);
和MySQL輸出爲:
Query OK, 41898 rows affected, 20948 warnings (0.78 sec)
Records: 20949 Deleted: 20949 Skipped: 0 Warnings: 20948
線數只有20949,但MySQL的報告爲41898行的影響。爲什麼這樣?此外,表中沒有真正改變。我也看不出所產生的警告是什麼。我想使用LOAD DATA INFILE,因爲它需要python半秒來更新每行,對於具有20,000個以上記錄的文件,其轉換爲2.77小時。
UPDATE:修改代碼來設置自動提交到 '假',並增加了db.commit()語句:
# Tell MySQLdb to turn off auto-commit
db.autocommit(False)
# Set count to 1
count = 1
while count < len(contents):
if contents[count][0] != '':
cursor.execute("""
UPDATE accounts SET cf_580 = %s, cf_568 = %s, cf_569 = %s, cf_571 = %s, cf_572 = %s
WHERE cf_535 = %s""" % (contents[count][1], contents[count][2], contents[count][3], contents[count][4], contents[count][5], contents[count][0]))
count += 1
try:
db.commit()
except:
db.rollback()
是的,我做的Python在插入單個語句,因爲該表是MyISAM和沒有按不支持交易。 2.謝謝你指出這一點。我忽略了這個。 我想我必須進一步解釋我想要的最終結果。我想通過首先匹配帳號並更新該帳號的值來更新數據庫。這將會定期完成。但是,從它的外觀來看,LOAD DATA INFILE適用於初始導入,不適合定期更新。 – Francis 2009-08-06 05:03:18
將MyISAM錶轉換爲InnoDB(忘記InnoDB在本地開發機器上被跳過/未加載)。 – Francis 2009-08-06 07:02:30