最近學習了下mysql中資料的匯入匯出,發現功能點真是豐富,很方便很快捷。
這些匯入匯出的方式還是有不少的細節的,在此先不做擴充套件和深入分析。
--資料匯出 方式1
比如要實現資料的匯出,直接可以指定生成的檔案使用outfile即可。對於空值的處理是「\n"
mysql> select * from test into outfile '/u02/mysql/dump/a.sql';
query ok, 4 rows affected (0.00 sec)
1 aaaa
2 bbbb
3 c
4 \n
--資料匯出 方式2
如果需要匯出某個資料庫下的表結構和資料,使用mysqldump也是不錯的選擇。生成的檔案中,.txt是對應的資料,.sql是對應的表結構sql。
mysql> show tables;
+----------------+
| tables_in_test |
+----------------+
| test |
| test1 |
+----------------+
2 rows in set (0.00 sec)
mysqldump -t /u02/mysql/dump -u root test
匯出後檢視目錄結構。
[mysql@oel1 dump]$ ll
total 8
-rw-r--r-- 1 mysql dba 1338 apr 11 22:39 test1.sql
-rw-rw-rw- 1 mysql dba 0 apr 11 22:39 test1.txt
-rw-r--r-- 1 mysql dba 1364 apr 11 22:39 test.sql
-rw-rw-rw- 1 mysql dba 0 apr 11 22:39 test.txt
--資料匯出 方式3
這種方式,直接把sql語句通過引數傳入,確實很讚。注意這個時候生成的空值是null,而不是"\n"
[mysql@oel1 dump]$ mysql -u root --execute="select *from test;" test > aa.sql
[mysql@oel1 dump]$ cat aa.sql
id name
1 aaaa
2 bbbb
3 c
4 null
[mysql@oel1 dump]$ cat ../a.sql
1 aaaa
2 bbbb
3 c
4 \n
--資料匯出 方式4
如果這個時候需要匯出的表中列較多,可以通過鍵值對的方式縱向顯示。可讀性就大大增強了。比如下面的方式。
[mysql@oel1 dump]$ mysql -u root
--vertical --execute="select *from
test;" test > aa.sql
[mysql@oel1 dump]$ cat aa.sql
*************************** 1. row ***************************
id: 1
name: aaaa
*************************** 2. row ***************************
id: 2
name: bbbb
*************************** 3. row ***************************
id: 3
name: c
*************************** 4. row ***************************
id: 4
name: null
注意這個時候,我指定sql語句以」;"結尾,和不加結尾符的效果是一樣的,可見這個時候還是有一定的健壯性。
[mysql@oel1 dump]$ mysql -u root --vertical --execute="select *from
test" test > aa.sql
[mysql@oel1 dump]$ cat aa.sql
*************************** 1. row ***************************
id: 1
name: aaaa
*************************** 2. row ***************************
id: 2
name: bbbb
*************************** 3. row ***************************
id: 3
name: c
*************************** 4. row ***************************
id: 4
name: null
--資料匯出 方式5
如果需要匯出的資料為html格式,也很清晰。直接呼叫-html選項即可。
mysql -u root --html --execute="select *from test" test > aa.html
mysql@oel1 dump]$ cat aa.html
id name
1 aaaa
2 bbbb
3 c
4 null
[mysql@oel1 dump]$
--資料匯出 方式6
如果指定匯出資料格式為xml,直接呼叫-xml選項即可。
[mysql@oel1 dump]$ mysql -u root --xml --execute="select *from test" test > aa.html
[mysql@oel1 dump]$ cat aa.html 1
aaaa 2
bbbb 3
c 4--資料匯出 方式7
如果希望把資料的結果快取出來,使用-tee也是一種選擇,這種方式會匯出所有的操作結果,可以根據自己的需要來做匯出。
mysql> tee a.log
logging to file 'a.log'
mysql> select *from test;
empty set (0.00 sec)
mysql> select *from test1;
empty set (0.00 sec)
mysql> notee;
outfile disabled.
這個時候會把sql和對應的資料結果都生成在日誌裡面。
--資料匯入 方式1
資料的匯入可以在mysql命令裡面使用load data的方式來實現,也是比較輕巧的。
mysql> load data infile '/u02/mysql/dump/a.sql' into table test.test ;
query ok, 4 rows affected (0.01 sec)
records: 4 deleted: 0 skipped: 0 warnings: 0
mysql> select *from test;
+------+------+
| id | name |
+------+------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | c |
| 4 | null |
| 1 | aaaa |
| 2 | bbbb |
| 3 | c |
| 4 | null |
+------+------+
8 rows in set (0.00 sec)
--資料匯入 方式2
資料匯入還可以使用mysqlimport來實現,比如我們需要匯入的表為test,資料檔案為test.txt
[mysql@oel1 dump]$ mysqlimport -u root test '/u02/mysql/dump/test.txt'
test.test: records: 0 deleted: 0 skipped: 0 warnings: 0
nodejs 牛刀小試
目前在做乙個使用者登入驗證的案例 真是彎路 彎路 再彎路啊 不多說上 用到的模板 express 資料庫mysql 使用者校驗 socket.on judgeuser function nickname function err1,rows if rows.length 0 else 使用者註冊 s...
查詢 牛刀小試
給定乙個值k,在含有n個記錄的表中找出關鍵字等於k的記錄。若找到則返回該記錄的的位置或資訊,否則查詢失敗,返回相關的指示資訊。一 順序查詢 思路 元素沒有順序,因此要掃瞄整個陣列中的所有元素,逐個與k比較 相等則查詢成功,返回該元素位置 如果掃瞄結束沒有發現和k相等的元素,返回0 int searc...
dynamic 牛刀小試
今天在網上看帖時看到了這樣乙個問題 現在我用反射 personmodel.dll 呼叫乙個型別方法 getallpersons 返回person,其中person為 personmodel.dll 在定義,請問,我要怎麼操作才能取回返回的陣列值呢?恰好手頭沒事做,就順手寫了乙個demo,但在獲取到結...