csv和excel导入mysql_把csv/excel/txt中的数据导入到mysql

1.ubuntu18.04上安装mysql,详情查阅转载:https://www.cnblogs.com/opsprobe/p/9126864.html

2.进入mysql

mysql -uroot -p ---输入密码后进入mysql

2.创建数据库

create database test;

3.创建表

create table dataset_video(filename varchar(128),duration float,label varchar(128),width int(11),height int(11),code_rate int(11),frame_rate float,package_format  varchar(11),code_format varchar(11) )character set=utf-8

创建后发现还需要添加两个字段ID和path,于是添加这两个字段:ALTER table dateset_video ADD (ID varchar(32) path text) character set=utf8;

但发现位置放在了最后,我希望位置是前两个,试了把ID放在第一位但是都不成功,后来曲线救国,删除了ID,重新添加在first,path放在了ID的后面,如下:

显示表结构:show full columns from dataset_video;

删除字段:alter table dataset_video drop column ID;

增加字段在首位:alter table dataset_video add column ID varchar(32) NOT NULL first;

把ID设为主键:alter table dataset_video addprimary key(ID);(主键要求NOT NULL)

调整表中字段位置:alter table dataset_video change path  path text after ID;

修改字段类型:alter  table dataset_video  modify  column duration  float;

NULL

4.导入库内容的准备工作

开始尝试导入csv和excel都不行,导入的除了ID其他都是NULL,于是查资料发现个很好的博客文章:转载https://www.cnblogs.com/zh1989/p/5856171.html。

把之前的csv进行备份,操作的那份进行另存为unicode文本(txt),用notepad++打开保存的txt文本,修改编码格式为utf-8, 把标题行删除只留数据行,放到服务器上

5.导入(txt文件中数据导入到mysql,字段内容要对应)

执行导入命令:

load data infile '/home/lq/mysql_video.txt' into table dataset_video lines terminated by '\r\n';

报错:ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

原来是权限问题,查看show global variables like '%secure_file_priv%'; (mysql下),得到

+------------------+-----------------------+

| Variable_name    | Value                 |

+------------------+-----------------------+

| secure_file_priv | /var/lib/mysql-files/ |

+------------------+-----------------------+

secure_file_priv 为 NULL 时,表示限制mysqld不允许导入或导出。

secure_file_priv 为 /tmp 时,表示限制mysqld只能在/tmp目录中执行导入导出,其他目录不能执行。

secure_file_priv 没有值为空''时,表示不限制mysqld在任意目录的导入导出。

但我试图在/etc/mysql/my.cnf中添加secure_file_priv='',重启mysql服务,但是还是不生效,最后这样解决的:加了个local

mysql> load data localinfile '/home/realai/liqing/mysql_video.txt' into table dataset_video lines terminated by '\r\n';

但在我尝试导出数据库中数据到txt时在哪儿加local都不行了,只好屈服导出到secure_file_priv的这个目录了/var/lib/mysql-files/

select * from dataset_video into outfile '/var/lib/mysql-files/test_out.txt' fields terminated by ',' optionally enclosed by '"';   或者命令

select * into outfile '/var/lib/mysql-files/test_out1.txt' from dataset_video limit 10;

续:2020.12.11

6.解决数据库中中文内容显示乱码的问题

问题:发现我导入的数据中的中文成乱码了

解决方法:

show   variables   like   "%char%";

+--------------------------------------+----------------------------+

| Variable_name                        | Value                     |

+--------------------------------------+----------------------------+

| character_set_client                 | utf8                      |

| character_set_connection             | utf8                   |

|character_set_database               | latin1               |

| character_set_filesystem             | binary                  |

| character_set_results                | utf8                       |

| character_set_server                 | latin1                  |

| character_set_system                 | utf8                      |

| character_sets_dir                   | /usr/share/mysql/charsets/ |

| validate_password_special_char_count | 1                  |

需要由latin1修改为utf-8

set character_set_server=utf8;

set character_set_database=utf8;

这是再重新导入数据,中文不是乱码了

但注意mysql重启好后又恢复成latin1了,从网上找了改配置文件的方法但是不生效,还需要继续找解决办法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值