zoukankan      html  css  js  c++  java
  • Mysql—数据导入与导出

    数据导入

    作用:把文件系统里的内容导入到数据库表中。

    语法:

    mysql> load data infile "文件名" into table 表名 fields terminated by "分隔符" lines terminated by "
    ";
    mysql> load data local infile "文件名" into table 表名 fields terminated by "分隔符" lines terminated by "
    ";

    示例:把 /www/wwwroot/student.txt 文件中的内容导入到 shop 数据库下的 tb_users 表中。

         文件内容为:1;"张三";123 2;"李四";456

    -- 1.先在数据库中创建对应的表
    create table tb_users( 
        userid int(11) NOT NULL,
        username varchar(32) NOT NULL,
        password varchar(256) NOT NULL,
    )engine=innodb default charset=utf8 comment="用户表";
    -- 2.执行数据导入语句
    mysql> load data infile "/www/wwwroot/student.txt" into table tb_users fields terminated by ";" lines terminated by " ";
    mysql> load data local infile "/www/wwwroot/student.txt" into table tb_users fields terminated by ";" lines terminated by " ";

    总结:向数据库导入TXT文件,需要先手动创建一个对应的数据表(和TXT文件数据格式保持一致),然后加载本地TXT文件,最后再直接插入到数据表中。这里MySQL数据库默认会按照Tab进行分割,如果是其他分隔符的话,可以使用fields terminated by关键字指定,逗号的话,是fields terminated by ',',分号是fields terminated by ';'。

    数据导出

    作用:把数据库表中的记录保存到系统文件里。

    语法:

    mysql> select ... from 表名 into outfile "文件名" fields terminated by "分隔符" lines terminated by "
    ";

    示例:把表中的 username 导出到文件user.txt(可以单独导出数据表某个字段的数据)

    mysql> select * from tb_users into outfile "/root/user.txt" fields terminated by "," lines terminated by "
    ";
    mysql> select username from tb_users into outfile "/root/user.txt" fields terminated by "," lines terminated by "
    ";
    

    注意:导出的内容由SQL查询语句决定。执行导出命令时路径必须指定对应的数据库搜索路径。 

    -- 查看数据库文件的路径
    mysql> show variables like "%datadir%";
    mysql> show global variables like '%datadir%';
    

      

  • 相关阅读:
    在Ubuntu上安装Hadoop(集群模式)
    Node.js v0.10.8 发布
    设置 Sublime Text 的 Python 开发环境
    jQuery 1.10.0 和 2.0.1 发布
    openSUSE 13.1 Milestone 2 发布
    mochad 0.1.6 发布,TCP 网关守护进程
    JPPF 3.3.2 发布,Java 并行处理框架
    PyCharm 又一强大Python IDE
    AntiXSS 支持Html同时防止XSS攻击
    (原创)攻击方式学习系列(总)
  • 原文地址:https://www.cnblogs.com/liuhaidon/p/11527370.html
Copyright © 2011-2022 走看看