当前位置:Gxlcms > 数据库问题 > 2-17作业 数据库和shell综合练习

2-17作业 数据库和shell综合练习

时间:2021-07-01 10:21:17 帮助过:2人阅读

2016-10 #translate Convert a newline symbol, # Write SQL sentences such as insert into table_name values(a,b); file=/home/yy/ #convert \r to ‘‘ sed -i s/\r// "$file"12306.txt mysql -e create database if not exists 12306_data; -usystem -p123456 echo DROP TABLE IF EXISTS uPwd_12306;>"$file"12306_mid.sql echo "CREATE TABLE uPwd_12306 ( uid int primary key auto_increment, uname varchar(40) not null default ‘‘, paw varchar(25) not null DEFAULT ‘‘, realname varchar(30) not null DEFAULT ‘‘, gmsfhm varchar(18) not null DEFAULT ‘‘, other varchar(25) not null DEFAULT ‘‘, tel varchar(11) not null DEFAULT ‘‘, email varchar(40) not null DEFAULT ‘‘ );">>"$file"12306_mid.sql sed s/----/\,\‘/g "$file"12306.txt |awk {print "insert into uPwd_12306(uname,paw,realname,gmsfhm,other,tel,email) values(‘\‘‘"$0"‘\‘‘);"}>>"$file"12306_mid.sql mysql -usystem -p123456 12306_data < "$file"12306_mid.sql View Code

技术分享

 运行报错

技术分享

上网查了下原因,因为数据太多。要修改一下下面的配置,对应linux是my.cnf

 技术分享

重启mysql

[root@ygy130 ~]# service mysqld restart

再次运行

[root@ygy130 ~]# ./make_12306_table.sh

ERROR 1064 (42000) at line 2279: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘210504197710140512‘,‘licheng1977‘,‘13942443523‘,‘licheng19771014@163.com‘);

报错,我查看一下中间文件

技术分享

发现这条数据是中文是乱码。

明明已经转了编码,为什么有的字是乱码。

 

经过研究,原先上传的12306.txt的编码是utf8,使用notepad++打开,中文是乱码,

将格式转成ansi后,中文能正确显示出来,因此,将显示正确的内容剪切,然后调整编码格式为utf8,再把剪切的内容粘贴上去。

 

然后再把这个转码后的12306.txt上传到linux。

这个方法可行,尝试的其他方法都没有行得通。

再次运行

[root@ygy130 ~]# ./make_12306_table.sh 

经过等待后,进入mysql验证一下。

 技术分享

验证成功!

2.

mysql> create table if not exists uPwd  ( uid int(10) primary key, upwd varchar(25), email varchar(40));

 技术分享

导入数据

mysql> insert into uPwd ( uid, upwd, email)select uid,paw,email from uPwd_12306;

Query OK, 131653 rows affected (0.52 sec)

Records: 131653  Duplicates: 0  Warnings: 0

3. mysql> select * from uPwd where email like ‘4145965‘;

4. mysql> create view uPwd_90 as select * from uPwd_12306 where substring(gmsfhm,7,4)>=1990;

Query OK, 0 rows affected (0.00 sec)

技术分享

 

2-17作业 数据库和shell综合练习

标签:

人气教程排行