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%';
    

      

  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    椭圆曲线加解密
    执行计划多版本查看
    椭圆曲线算法:入门(1)
    “戏精少女”的pandas学习之路,你该这么学!No.5
    用Fabric构建应收账款融资系统的方法
    区块链的去中心化创新
    搜集统计信息
    去中心化计算
  • 原文地址:https://www.cnblogs.com/liuhaidon/p/11527370.html
Copyright © 2011-2022 走看看