發(fā)表時間: 2006-12-17 16:45 作者: llehotnwod 來源: PHPChina 開源社區(qū)門戶mysql:
------------------------------------------------------------------ mysql> create table zy ( xuhao int(6) unsigned zerofill auto_increment primary key not null, fenqu char(50) character set utf8, bianma int(6) unsigned zerofill, liexin char(50) character set utf8, weizhi char(50) character set utf8 ); Query OK, 0 rows affected (0.62 sec) mysql> describe zy; +--------+--------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------------------+------+-----+---------+----------------+ | xuhao | int(6) unsigned zerofill | NO | PRI | NULL | auto_increment | | fenqu | char(50) | YES | | NULL | | | bianma | int(6) unsigned zerofill | YES | | NULL | | | liexin | char(50) | YES | | NULL | | | weizhi | char(50) | YES | | NULL | | +--------+--------------------------+------+-----+---------+----------------+ 5 rows in set (0.28 sec) ------------------------------------------------------------------ 輸入文件: file ~/doc /home/soj/doc: UTF-8 Unicode text cat ~/doc 部分內(nèi)容 283 碌?露鎂路??酶 020179 ?廬?梅?賂?戮 露鎂虜茫?梅?擄?酶 284 碌?露鎂路??酶 020180 ??潞?碌煤路摟 露鎂虜茫?梅?擄?酶 285 碌?露鎂路??酶 020181 路?祿冒路摟 露鎂虜茫?梅?擄?酶 286 碌?露鎂路??酶 020182 路?祿冒路摟 露鎂虜茫?梅?擄?酶 事先我用iconv將其轉(zhuǎn)成了utf-8,在C中實在是不會,sigh~~~~ 程序代碼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"); if( 0 != mysql_options(&mysql,MYSQL_SET_CHARSET_NAME,"utf8")){ fprintf(stderr,"mysql_options Error: %s\n",mysql_error(&mysql)); } if( 0 != mysql_set_character_set(&mysql,"utf8") ){ fprintf(stderr,"mysql_set_character_set Error: %s\n",mysql_error(&mysql)); } /* --------------------------------- */ /* 021 第一分區(qū) 010021 光電感煙 一層東后區(qū) */ 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); } 編譯: gcc -std=c99 -L /usr/local/lib/mysql/ -lmysqlclient -I /usr/local/include/mysql/ main.c -o sql 查看結(jié)果: mysql> select * from zy; +--------+----------+--------+----------+----------------+ | xuhao | fenqu | bianma | liexin | weizhi | +--------+----------+--------+----------+----------------+ | 000001 | 第一分區(qū) | 001001 | 光電感煙 | 二層東后區(qū) | | 000002 | 第一分區(qū) | 001002 | 光電感煙 | 二層東后區(qū) | | 000003 | 第一分區(qū) | 001003 | 光電感煙 | 二層東后區(qū) | | 000004 | 第一分區(qū) | 001004 | 光電感煙 | 二層東后區(qū) | | 000005 | 第一分區(qū) | 001005 | 光電感煙 | 二層東后區(qū) | | 000006 | 第一分區(qū) | 001006 | 光電感煙 | 二層東后區(qū) | | 000007 | 第一分區(qū) | 001007 | 光電感煙 | 二層東后區(qū) | | 000008 | 第一分區(qū) | 001008 | 光電感煙 | 二層東后區(qū) | | 000009 | 第一分區(qū) | 001009 | 光電感煙 | 二層東后區(qū) | ................................................................. | 000285 | 第二分區(qū) | 020181 | 防火閥 | 二層西前區(qū) | | 000286 | 第二分區(qū) | 020182 | 防火閥 | 二層西前區(qū) | +--------+----------+--------+----------+----------------+ |
|
來自: larrin > 《數(shù)據(jù)庫》