2015-09-14 48 views
-6

我是Python的新手,我正在使用python 3.4,並且想編寫一個腳本從csv文件(只有2個cols- emp_id,emp_name)加載數據到一個MySQL數據庫表。任何人都有示例代碼?請幫忙。Python 3.4代碼來加載一個csv文件到MySql

+0

嗨。我們不是代碼編寫或推薦服務。 –

+0

歡迎來到堆棧溢出!你似乎在要求某人爲你寫一些代碼。堆棧溢出是一個問答網站,而不是代碼寫入服務。請[see here](http://stackoverflow.com/help/how-to-ask)學習如何編寫有效的問題。 –

回答

1

你不需要Python。你甚至不需要一個程序。 MySQL有一個讀取CSV文件的語句。

見:https://dev.mysql.com/doc/refman/5.1/en/load-data.html,從文檔:

LOAD DATA INFILE can be used to read files obtained from external sources. For 
example, many programs can export data in comma-separated values (CSV) format, 
such that lines have fields separated by commas and enclosed within double 
quotation marks, with an initial line of column names. If the lines in such a 
file are terminated by carriage return/newline pairs, the statement shown here 
illustrates the field- and line-handling options you would use to load the file: 

LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name 
    FIELDS TERMINATED BY ',' ENCLOSED BY '"' 
    LINES TERMINATED BY '\r\n' 
    IGNORE 1 LINES; 

If the input values are not necessarily enclosed within quotation marks, use 
OPTIONALLY before the ENCLOSED BY keywords. 

除此之外,MySQL還自帶了一個叫做mysqlimport程序,可以做到這一點。