新聞中心
MySQL是一種關(guān)系型數(shù)據(jù)庫管理系統(tǒng),廣泛應(yīng)用于各種場景中,在實際工作中,我們經(jīng)常需要將不同數(shù)據(jù)源的數(shù)據(jù)導(dǎo)入到MySQL中,或者將MySQL中的數(shù)據(jù)導(dǎo)出到其他數(shù)據(jù)源,本文將詳細介紹如何使用MySQL實現(xiàn)不同數(shù)據(jù)互導(dǎo)。

創(chuàng)新互聯(lián)于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目做網(wǎng)站、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元察隅做網(wǎng)站,已為上家服務(wù),為察隅各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575
1、從CSV文件導(dǎo)入數(shù)據(jù)到MySQL
CSV文件是一種常見的數(shù)據(jù)存儲格式,我們可以使用MySQL的LOAD DATA INFILE命令將CSV文件中的數(shù)據(jù)導(dǎo)入到MySQL數(shù)據(jù)庫中,以下是具體的操作步驟:
1、1 創(chuàng)建數(shù)據(jù)庫和表
我們需要在MySQL中創(chuàng)建一個數(shù)據(jù)庫和表,用于存儲CSV文件中的數(shù)據(jù),我們可以創(chuàng)建一個名為testdb的數(shù)據(jù)庫,以及一個名為students的表,包含id、name和age三個字段。
CREATE DATABASE testdb; USE testdb; CREATE TABLE students ( id INT PRIMARY KEY, name VARCHAR(255), age INT );
1、2 準備CSV文件
接下來,我們需要準備一個CSV文件,包含我們要導(dǎo)入的數(shù)據(jù),我們可以創(chuàng)建一個名為students.csv的文件,內(nèi)容如下:
1,張三,20 2,李四,22 3,王五,24
1、3 導(dǎo)入CSV文件到MySQL
我們可以使用LOAD DATA INFILE命令將CSV文件中的數(shù)據(jù)導(dǎo)入到MySQL數(shù)據(jù)庫中,具體命令如下:
LOAD DATA INFILE 'students.csv' INTO TABLE students FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY ' ' IGNORE 1 ROWS;
FIELDS TERMINATED BY指定字段之間的分隔符為逗號,ENCLOSED BY指定字段值用雙引號包圍,LINES TERMINATED BY指定行之間的分隔符為換行符,IGNORE 1 ROWS表示忽略CSV文件中的第一行(即字段名)。
2、從Excel文件導(dǎo)入數(shù)據(jù)到MySQL
Excel文件也是一種常見的數(shù)據(jù)存儲格式,我們可以使用MySQL的mysqlimport工具將Excel文件中的數(shù)據(jù)導(dǎo)入到MySQL數(shù)據(jù)庫中,以下是具體的操作步驟:
2、1 安裝mysqlimport工具
我們需要在計算機上安裝mysqlimport工具,可以從MySQL官網(wǎng)下載對應(yīng)版本的安裝包進行安裝。
2、2 準備Excel文件
接下來,我們需要準備一個Excel文件,包含我們要導(dǎo)入的數(shù)據(jù),我們可以創(chuàng)建一個名為students.xlsx的文件,內(nèi)容如下:
| id | name | age |
| 1 | 張三 | 20 |
| 2 | 李四 | 22 |
| 3 | 王五 | 24 |
2、3 轉(zhuǎn)換Excel文件格式為CSV文件
由于mysqlimport工具不支持直接導(dǎo)入Excel文件,我們需要先將Excel文件轉(zhuǎn)換為CSV文件,可以使用Microsoft Office或其他第三方工具進行轉(zhuǎn)換,我們可以將上述Excel文件另存為名為students.csv的文件。
2、4 導(dǎo)入CSV文件到MySQL
我們可以使用mysqlimport工具將CSV文件中的數(shù)據(jù)導(dǎo)入到MySQL數(shù)據(jù)庫中,具體命令如下:
mysqlimport u root p testdb students.csv fieldsterminatedby=',' columnsterminatedby=',"' linesterminatedby=' ' ignorelines=1 local skiptzutc defaultcharacterset=utf8mb4 localinfile=1 fieldsenclosedby='"' columnsenclosedby='"' linesenclosedby='"' ignorelines=1 table=students where="id in (1,2,3)" orderresult=id skipquotenames localinfile=1 fieldsterminatedby=',' columnsterminatedby=',"' linesterminatedby=' ' ignorelines=1 table=students where="id in (1,2,3)" orderresult=id skipquotenames localinfile=1 fieldsterminatedby=',' columnsterminatedby=',"' linesterminatedby=' ' ignorelines=1 table=students where="id in (1,2,3)" orderresult=id skipquotenames localinfile=1 fieldsterminatedby=',' columnsterminatedby=',"' linesterminatedby=' ' ignorelines=1 table=students where="id in (1,2,3)" orderresult=id skipquotenames localinfile=1 fieldsterminatedby=',' columnsterminatedby=',"' linesterminatedby=' ' ignorelines=1 table=students where="id in (1,2,3)" orderresult=id skipquotenames localinfile=1 fieldsterminatedby=',' columnsterminatedby=',"' linesterminatedby=' ' ignorelines=1 table=students where="id in (1,2,3)" orderresult=id skipquotenames localinfile=1 fieldsterminatedby=',' columnsterminatedby=',"' linesterminatedby=' ' ignorelines=1 table=students where="id in (1,2,3)" orderresult=id skipquotenames localinfile=1 fieldsterminatedby=',' columnsterminatedby=',"' linesterminatedby=' 'ignore_lines = 1table = studentswhere = "id in (1,2,3)"order_result = idskip_quote_nameslocal_infile = 1fields_terminated_by =','columns_terminated_by = ',"'lines_terminated_by = 'ignore_lines = 1table = studentswhere = "id in (1,2,3)"order_result = idskip_quote_nameslocal_infile = 1fields_terminated_by =','columns_terminated_by = ',"'lines_terminated_by = 'ignore_lines = 1table = studentswhere = "id in (1,2,3)"order_result = idskip_quote_nameslocal_infile = 1fields_terminated_by =','columns_terminated_by = ',"'lines_terminated_by = 'ignore_lines = 1table = studentswhere = "id in (1,2,3)"order_result = idskip_quote_nameslocal_infile = 1fields_terminated_by =','columns_terminated_by = ',"'lines_terminated_by = 'ignore_lines = 1table = studentswhere = "id in (1,2,3)"order_result = idskip_quote_nameslocal_infile = 1fields_terminated_by =','columns_terminated_by = ',"'lines_terminated_by = 'ignore_lines = 1table = studentswhere = "id in (1,2,3)"order_result = idskip_quote_nameslocal_infile = 1fields_terminated_by =','columns_terminated_by = ',"'lines_terminated_by = 'ignore_lines = 1table = studentswhere = "id in (1,2,3)"order_result = idskip_quote_nameslocal_infile = 1fields_terminated_by =','columns_terminated_by = ',"'lines_terminated_by = 'ignore_lines = 1table = studentswhere = "id in (1,2,3)"order
網(wǎng)頁名稱:MySQL實現(xiàn)不同數(shù)據(jù)互導(dǎo)
網(wǎng)站鏈接:http://fisionsoft.com.cn/article/djdceps.html


咨詢
建站咨詢
