MySql的mysqlimport 工具

不使用用户名/密码不可能在命令行下执行MySql的工具向数据库中导入数据,就像没有钥匙怎么打开房门
+++++++++++++++++++++++++
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport Meet_A_Geek customers.txt
mysqlimport: Error: 1045 Access denied for user 'ODBC'@'localhost' (using password: NO)

+++++++++++++++++++++++++
数据库中还没有这张表呢,怎么向里面灌数据,以什么格式、什么规则存放数据?
+++++++++++++++++++++++++
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd Meet_A_Geek customers.txt
mysqlimport: Error: 1146, Table 'meet_a_geek.customers' doesn't exist, when using table: customers

+++++++++++++++++++++++++
在MySql中建表
mysql> use Meet_A_Geek
Database changed
mysql> create table customers(name_test varchar(20));
Query OK, 0 rows affected (0.24 sec)

mysql> select * from Customers; ------------------------>证明MySql不区分大小写
Empty set (0.00 sec)

mysql>
++++++++++++++++++++++++
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd Meet_A_Geek customers.txt
mysqlimport: Error: 29, File 'C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5
.1\Data\meet_a_geek\customers.txt' not found (Errcode: 2), when using table: customers

----------------〉此时我的customers.txt文档位于C:\Program Files\MySQL\MySQL Server 5.1\bin文件夹下

把我的customers.txt文件放到D:\下,执行下面的CMD命令,不是在MySql命令提示符下,别晕了:)
++++++++++++++
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd Meet_A_Geek D:/customers.txt
Meet_A_Geek.customers: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
++++++++++++++
在MySql命令提示符下

mysql> select * from Customers;
+------------+
| name_test  |
+------------+
| 'abc_text' |
+------------+
1 row in set (0.00 sec)

mysql>

+++++++++++++
结果正确,'abc_test',正是我的customers.txt文件中的唯一内容。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mysqlimport的常用选项
①-d (--delete)   "新数据导入数据表中之前删除数据数据表中的所有信息"
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd -d Meet_A_Geek D:/customers.txt
Meet_A_Geek.customers: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd -delete Meet_A_Geek D:/custom
ers.txt
mysqlimport: unknown option '-e'
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd --delete Meet_A_Geek D:/custo
mers.txt
Meet_A_Geek.customers: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
②-f or --force 不管是否遇到错误,mysqlimport将强制继续插入数据
③-i or --ignore mysqlimport跳过或者忽略那些有相同唯一关键字的行, 导入文件中的数据将被忽略。
④-l or -lock-tables 数据被插入之前锁住表,这样就防止了, 你在更新数据库时,用户的查询和更新受到影响
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd -l Meet_A_Geek D:/customers.txt
Meet_A_Geek.customers: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd -lock-tables Meet_A_Geek D:/customers.txt
mysqlimport: unknown option '-o'
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd -locktables Meet_A_Geek D:/customers.txt
mysqlimport: unknown option '-o'

⑤-r or -replace 这个选项与-i选项的作用相反;此选项将替代表中有相同唯一关键字的记录。
⑥--fields-enclosed- by= char
  指定文本文件中数据的记录时以什么括起的, 很多情况下数据以双引号括起。 默认的情况下数据是没有被字符括起的。
⑦--fields-terminated- by=char
  指定各个数据的值之间的分隔符,在句号分隔的文件中,分隔符是句号。您可以用此选项指定数据之间的分隔符。默认的分隔符是跳格符(Tab)
⑧--lines-terminated- by=str
  此选项指定文本文件中行与行之间数据的分隔字符串 或者字符。 默认的情况下mysqlimport以newline为行分隔符。 您可以选择用一个字符串来替代一个单个的字符: 一个新行或者一个回车。
⑨mysqlimport命令常用的选项还有-v 显示版本(version), -p 提示输入密码(password)等。
C:\Program Files\MySQL\MySQL Server 5.1\bin>mysqlimport -uroot -pp@ssw0rd -l -v Meet_A_Geek D:/customers.txt
Connecting to localhost
Selecting database Meet_A_Geek
Locking tables for write
Loading data from SERVER file: D:/customers.txt into customers
Meet_A_Geek.customers: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
Disconnecting from localhost

3).例子:导入一个以逗号为分隔符的文件

  文件中行的记录格式是这样的:
  "1", "ORD89876", "1 Dozen Roses", "19991226"
  我们的任务是要把这个文件里面的数据导入到数据库Meet_A_Geek中的表格Orders中, 我们使用这个命令:
  bin/mysqlimport –prl –fields-enclosed-by=" –fields-terminated-by=, Meet_A_Geek Orders.txt
  这个命令可能看起来很不爽,不过当你熟悉了之后,这是非常简单的。第一部分,bin/mysqlimport ,告诉操作系统你要运行的命令是mysql/bin目录下的mysqlimport,选项p是要求输入密码,这样就要求你在改动数据库之前输入密码,操作起来会更安全。 我们用了r选项是因为我们想要把表中的唯一关键字与文件记录中有重复唯一关键字的记录替换成文件中的数据。我们表单中的数据不是最新的,需要用文件中的数据去更新,因而就用r这个选项,替代数据库中已经有的记录。l选项的作用是在我们插入数据的时候锁住表,这样就阻止了用户在我们更新表的时候对表进行查询或者更改的操作
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值