c连MySQL ,insert后数据乱码了

avalonBBS 2006-12-17 10:22:13
:~> mysql -u zhongyi -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22 to server version: 5.1.12-beta

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use ZhongYi
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> describe zy;

+--------+--------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------------+------+-----+---------+----------------+
| xuhao | int(5) unsigned zerofill | NO | PRI | NULL | auto_increment |
| fenqu | char(30) | YES | | NULL | |
| bianma | int(6) unsigned zerofill | YES | | NULL | |
| liexin | char(30) | YES | | NULL | |
| weizhi | char(30) | YES | | NULL | |
+--------+--------------------------+------+-----+---------+----------------+

5 rows in set (0.15 sec)


cat 中宜编程资料.doc

001 第一分区 01001 光电感烟 二层东后区
002 第一分区 01002 光电感烟 二层东后区

.................................



file 中宜编程资料.doc
中宜编程资料.doc: ISO-8859 text



main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
//#include <sys/iconv.h>
//#include "/usr/src/sys/sys/iconv.h"
#include "/usr/src/bin/csh/iconv.h"
#include "/usr/local/include/mysql/mysql.h"

int
main(int argc,char **argv)
{
MYSQL mysql;
FILE *fp;

if( 2 != argc ){
fprintf(stderr,"Usage:./prog inputfile\n");
exit(-1);
}

if( NULL == (fp=fopen(argv[1],"r")) ){
fprintf(stderr,"%s\n",strerror(errno));
exit(-1);
}
fprintf(stdout,"file %s opend...\n",argv[1]);

mysql_init(&mysql);
if(NULL==mysql_real_connect(&mysql,"localhost","zhongyi","localhost","ZhongYi",0,NULL,0)){
fprintf(stderr,"Failed to connect to database: Error: %s\n",mysql_error(&mysql));
exit(-1);
}
fprintf(stdout,"DataBase Connected...\n");

/* --------------------------------- */
/* 021 第一分区 010021 光电感烟 一层东后区 */
for(char buf[4096]; NULL != fgets(buf,4096,fp); ){
/*
* | fenqu | char(30)
* | bianma | int(6) unsigned zerofill
* | liexin | char(30)
* | weizhi | char(30)
*/
char query[1024]="INSERT INTO zy(fenqu,bianma,liexin,weizhi) VALUES('";
char *iter_b,*iter_e;

for(iter_b=buf; 0 < *iter_b; ++iter_b);
for(iter_e=iter_b; 0 > *iter_e; iter_e+=2);
strncat(query,iter_b,iter_e-iter_b);
strcat(query,"',");
for(iter_b=iter_e; ' '==*iter_b; ++iter_b);
for(iter_e=iter_b; ' '!=*iter_e; ++iter_e);
strncat(query,iter_b,iter_e-iter_b);
strcat(query,",'");
for(iter_b=iter_e; 0 < *iter_b; ++iter_b);
for(iter_e=iter_b; 0 > *iter_e; iter_e+=2);
strncat(query,iter_b,iter_e-iter_b);
strcat(query,"','");
for(iter_b=iter_e; 0 < *iter_b; ++iter_b);
for(iter_e=iter_b; 0 > *iter_e; iter_e+=2);
strncat(query,iter_b,iter_e-iter_b);
strcat(query,"')");

/* iconv */
iconv_t cd;
char outbuf[1024];
size_t in=strlen(query),out=1024;

if( (iconv_t)-1 == (cd = iconv_open("UTF-8","GBK")) ){
fprintf(stderr,"Iconv_open Error: %s\n",strerror(errno));
exit(-1);
}
if( (size_t)-1 == iconv(cd,(char **)&query,(size_t *)&in,(char **)&outbuf,(size_t *)&out) ){
fprintf(stderr,"Iconv Eoor: %s\n",strerror(errno));
exit(-1);
}

if( mysql_real_query(&mysql,outbuf,out) ){
fprintf(stderr,"Error making query: %s\n",mysql_error(&mysql));
}

if( -1 == iconv_close(cd) ){
fprintf(stderr,"Iconv_close Error: %s\n",strerror(errno));
exit(-1);
}
}

fclose(fp);
mysql_close(&mysql);

exit(0);
}


上面代码中iconv部分我不知道怎么写,如果去掉那部分,程序可以运行,但插在zy数据库中的汉字部分全是乱码:cry:cry:cry
...全文
306 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
avalonBBS 2007-03-05
  • 打赏
  • 举报
回复
thx
福贵 2006-12-18
  • 打赏
  • 举报
回复
mysql默认的字符编码就是iso-8859-1
java里面这样转的
字符串.getBytes("iso-8859-1"),"要转成的字符编码")
avalonBBS 2006-12-17
  • 打赏
  • 举报
回复
我又改了一下,加了character and set names在程序里,这回又出现问号了 *___*

if( 0 != mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8")){
fprintf(stderr,"mysql_options Error: %s\n",mysql_error(&mysql));
exit(-1);
}
if( 0 != mysql_set_character_set(&mysql,"utf8") ){
fprintf(stderr,"mysql_set_character_set Error: %s\n",mysql_error(&mysql));
exit(-1);
}

/* --------------------------------- */
/* 021 第一分区 010021 光电感烟 一层东后区 */
for(char buf[4096]; NULL != fgets(buf,4096,fp); ){
/*
* | fenqu | char(30)
* | bianma | int(6) unsigned zerofill
* | liexin | char(30)
* | weizhi | char(30)
*/
char query[1024]="INSERT INTO zy(fenqu,bianma,liexin,weizhi) VALUES('";
char *iter_b,*iter_e;

for(iter_b=buf; 0 < *iter_b; ++iter_b);
for(iter_e=iter_b; 0 > *iter_e; iter_e+=2);
strncat(query,iter_b,iter_e-iter_b);
strcat(query,"',");
for(iter_b=iter_e; ' '==*iter_b; ++iter_b);
for(iter_e=iter_b; ' '!=*iter_e; ++iter_e);
strncat(query,iter_b,iter_e-iter_b);
strcat(query,",'");
for(iter_b=iter_e; 0 < *iter_b; ++iter_b);
for(iter_e=iter_b; 0 > *iter_e; iter_e+=2);
strncat(query,iter_b,iter_e-iter_b);
strcat(query,"','");
for(iter_b=iter_e; 0 < *iter_b; ++iter_b);
for(iter_e=iter_b; 0 > *iter_e; iter_e+=2);
strncat(query,iter_b,iter_e-iter_b);
strcat(query,"')");

/* iconv */
/*
iconv_t cd;
char outbuf[1024];
size_t in=strlen(query),out=1024;

if( (iconv_t)-1 == (cd = iconv_open("UTF-8","GBK")) ){
fprintf(stderr,"Iconv_open Error: %s\n",strerror(errno));
exit(-1);
}
if( (size_t)-1 == iconv(cd,(char **)&query,(size_t *)&in,(char **)&outbuf,(size_t *)&out) ){
fprintf(stderr,"Iconv Eoor: %s\n",strerror(errno));
exit(-1);
}
*/

if( mysql_real_query(&mysql,query,strlen(query)) ){
fprintf(stderr,"Error making query: %s\n",mysql_error(&mysql));
}

/*
if( -1 == iconv_close(cd) ){
fprintf(stderr,"Iconv_close Error: %s\n",strerror(errno));
exit(-1);
}
*/
}

fclose(fp);
mysql_close(&mysql);

exit(0);
}
jackeyabc 2006-12-17
  • 打赏
  • 举报
回复
不懂,帮顶~期望早点解决~
avalonBBS 2006-12-17
  • 打赏
  • 举报
回复
我加了这两行,依旧

if( 0 != mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8")){
fprintf(stderr,"mysql_options Error: %s\n",mysql_error(&mysql));
exit(-1);
}
if( 0 != mysql_set_character_set(&mysql,"utf8") ){
fprintf(stderr,"mysql_set_character_set Error: %s\n",mysql_error(&mysql));
exit(-1);
}
supergaucau 2006-12-17
  • 打赏
  • 举报
回复
要用utf8字符集,连接成功后执行"set names utf8"

56,677

社区成员

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

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