[英]can't insert utf-8 characters in mysql database using crontab to automate the program
I had asked the question before but because I didn't receive any guide, I'm asking it again. 我之前曾问过这个问题,但由于我没有得到任何指导,所以我再次询问。 Here's the original question: fail to insert non-english characters to database using crontab to run the program
这是原始问题: 无法使用crontab运行程序将非英语字符插入数据库
I wrote a java program that reads from a UTF-8 file and insert its contents to mysql database. 我编写了一个Java程序,该程序从UTF-8文件读取并将其内容插入mysql数据库。 I can call jar file in terminal and it works fine.
我可以在终端中调用jar文件,并且工作正常。 after that I used crontab to automate running the program but this time only english chars and numbers will insert to DB.
之后,我使用crontab来自动运行程序,但是这次只有英文字符和数字会插入DB。
My database encoding is "utf8-general-ci" and the server I'm running the program is centOs 6.3. 我的数据库编码是“ utf8-general-ci”,运行该程序的服务器是centOs 6.3。
I should mention that I have another program that its functionality is similar to this and it works fine. 我应该提到,我还有另一个程序,其功能与此相似,并且运行良好。
I have tested so many things but none of them fixed the problem. 我已经测试了很多东西,但是没有一个解决问题。 I don't know what's the problem.
我不知道出什么问题了。
I'm looking for a solution or any clue to continue and solve the problem. 我正在寻找解决方案或任何线索以继续解决问题。 I'll appreciate any help.
我将不胜感激。
EDIT: mysql version is :5.1.69 编辑:mysql版本是:5.1.69
and here is 2 images. 这是2张图片 one is for the time I run the program with crontab and the other when running the program in terminal
一种是我用crontab运行程序的时间,另一种是在终端中运行程序的时间
I'm not realy an expert on this but here goes nothing. 我不是真正的专家,但是这里什么也没有。 I think this is related to what's documented in http://dev.mysql.com/doc/refman/5.6/en/alter-table.html
我认为这与http://dev.mysql.com/doc/refman/5.6/en/alter-table.html中记录的内容有关
Is your default character set on latin1 or something like that ? 您在latin1上设置了默认字符还是类似的名称? Perhaps you left it default and i think it then uses latin1.
也许您将其保留为默认值,然后我认为它会使用latin1。
In any case i think you have to do something like this: 无论如何,我认为您必须执行以下操作:
ALTER TABLE target_table_name MODIFY latin1_text_col TEXT CHARACTER SET utf8;
ALTER TABLE target_table_name MODIFY latin1_varchar_col VARCHAR(M) CHARACTER SET utf8;
You can find out the default char set of the database by doing: 您可以通过执行以下操作找出数据库的默认字符集:
use db_name;
show variables like "character_set_database";
show variables like "collation_database";
I couldn't find anyway to fix the problem so I wrote a schedular program that runs the application and it works fine!!! 我仍然找不到解决该问题的方法,所以我编写了一个计划程序来运行该应用程序,并且运行良好!!!
But I'm still confused why crontab makes that problem or what I did that program doesn't work fine!!! 但是我仍然很困惑,为什么crontab会导致这个问题,或者我所做的该程序无法正常工作!!!
Assume that you have set you db, table, and columns using utf8mb4, don't forget specify your connection to mysql using utf8 charset : 假设您已使用utf8mb4设置了数据库,表和列, 请不要忘记使用utf8 charset指定与mysql的连接 :
String url = "jdbc:mysql://ip:port/db?characterEncoding=utf8";
String username = "xxx";
String password = "xxx";
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
-- -
I'm curious about the root cause of your problems as well... 我也很好奇您问题的根源...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.