crontab插入mysql乱码

chengchow2001 2014-09-19 11:46:50
我想用MYSQL记录一些城市的每天天气情况,数据是从中国天气网获取,通过脚本格式化数据
mysql -e 插入到数据库
数据库结构
mysql> desc weather;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| cityid | int(10) | NO | | NULL | |
| city | varchar(20) | NO | | NULL | |
| time | datetime | NO | | NULL | |
| temperature | varchar(10) | NO | | NULL | |
| windspeed | varchar(20) | NO | | NULL | |
| humidity | varchar(10) | YES | | NULL | |
+-------------+-------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)

数据库查询
mysql> select * from weather;
+----+-----------+---------------+---------------------+-------------+-----------------------------+----------+
| id | cityid | city | time | temperature | windspeed | humidity |
+----+-----------+---------------+---------------------+-------------+-----------------------------+----------+
| 1 | 101010100 | 北京 | 2014-09-18 20:20:14 | 21 | 西南风|2级 | 67% |
| 2 | 101020100 | 上海 | 2014-09-18 20:20:14 | 19 | 西北风|1级 | 89% |
| 3 | 101220101 | 合肥 | 2014-09-18 20:20:14 | 19 | 东风|2级 | 86% |
| 4 | 101220105 | 巢湖 | 2014-09-18 20:20:14 | 18 | 东南风|1级 | 92% |
| 5 | 101280601 | 深圳 | 2014-09-18 20:20:14 | 32 | 西南风|1级 | 60% |
| 6 | 101010100 | 乱码 | 2014-09-18 20:21:01 | 21 | 乱码 | 66% |
| 7 | 101020100 | 乱码 | 2014-09-18 20:21:01 | 19 | 乱码 | 89% |
| 8 | 101220101 | 乱码 | 2014-09-18 20:21:01 | 19 | 乱码 | 86% |
| 9 | 101220105 | 乱码 | 2014-09-18 20:21:01 | 18 | 乱码 | 91% |
| 10 | 101280601 | 乱码 | 2014-09-18 20:21:01 | 33 | 乱码 | 62% |
其中
id 1-5是手动运行脚本(/bin/sh /root/geweather.sh)数据
id 6-10是crontab运行脚本导入的值,乱码不让上传,图片也被封了,用“乱码代替了”
crontab配置
*/1 * * * * root /bin/sh /root/weather/getweather.sh > /dev/null 2>&1

现在的情况就是我手动运行/bin/sh /root/weather/getweather.sh > /dev/null 2>&1插入数据中文可以显示
crontab执行中文就变成乱码了


...全文
153 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengchow2001 2014-09-23
  • 打赏
  • 举报
回复
引用 6 楼 ACMAIN_CHM 的回复:
脚本 中用 set names 设置一下字符集,然后再测试。
非常感谢,是这个问题,另外补充问下,手动运行为什么不需要设置set names utf8
ACMAIN_CHM 2014-09-22
  • 打赏
  • 举报
回复
脚本 中用 set names 设置一下字符集,然后再测试。
chengchow2001 2014-09-22
  • 打赏
  • 举报
回复
chengchow2001 2014-09-22
  • 打赏
  • 举报
回复

终于把图片上传上来了
chengchow2001 2014-09-22
  • 打赏
  • 举报
回复
引用 2 楼 ACMAIN_CHM 的回复:
按下贴中的方法检查字符集设置。并贴出以供分析。 http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx MySQL 中文显示乱码
文章我看了,好像和我的问题关系不大,我手动运行脚本是正常显示的,见表1-5行 只是把脚本放到crontab中,自动运行才会出现问题,见表6-10行 下面是我字符设置 show variables like 'char%'; +--------------------------+------------------------------------+ | Variable_name | Value | +--------------------------+------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /data/mysql-5.5.37/share/charsets/ | +--------------------------+------------------------------------+ 8 rows in set (0.00 sec)
ACMAIN_CHM 2014-09-21
  • 打赏
  • 举报
回复
按下贴中的方法检查字符集设置。并贴出以供分析。 http://blog.csdn.net/ACMAIN_CHM/archive/2009/05/12/4174186.aspx MySQL 中文显示乱码
ecton 2014-09-21
  • 打赏
  • 举报
回复
这个我遇到过,网上各种设置字符集,不过我建议还是装同一个mysql版本比较好。我是这样解决的。

56,673

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧