发表时间: 2006-12-17 16:45 作者: llehotnwod 来源: PHPChina 开源社区门户
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 部分内容
283 碌?露镁路??酶 020179 ?庐?梅?赂?戮
露镁虏茫?梅?掳?酶
284 碌?露镁路??酶 020180 ??潞?碌煤路搂
露镁虏茫?梅?掳?酶
285 碌?露镁路??酶 020181 路?禄冒路搂
露镁虏茫?梅?掳?酶
286 碌?露镁路??酶 020182 路?禄冒路搂
露镁虏茫?梅?掳?酶
事先我用iconv将其转成了utf-8,在C中实在是不会,sigh~~~~
程序代码main.c:
#include
#include
#include
#include
//#include
//#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 第一分区 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);
}
编译:
gcc -std=c99 -L /usr/local/lib/mysql/ -lmysqlclient -I
/usr/local/include/mysql/ main.c -o sql
查看结果:
mysql> select * from zy;
+--------+----------+--------+----------+----------------+
| xuhao | fenqu | bianma | liexin | weizhi |
+--------+----------+--------+----------+----------------+
| 000001 | 第一分区 | 001001 | 光电感烟 | 二层东后区 |
| 000002 | 第一分区 | 001002 | 光电感烟 | 二层东后区 |
| 000003 | 第一分区 | 001003 | 光电感烟 | 二层东后区 |
| 000004 | 第一分区 | 001004 | 光电感烟 | 二层东后区 |
| 000005 | 第一分区 | 001005 | 光电感烟 | 二层东后区 |
| 000006 | 第一分区 | 001006 | 光电感烟 | 二层东后区 |
| 000007 | 第一分区 | 001007 | 光电感烟 | 二层东后区 |
| 000008 | 第一分区 | 001008 | 光电感烟 | 二层东后区 |
| 000009 | 第一分区 | 001009 | 光电感烟 | 二层东后区 |
.................................................................
| 000285 | 第二分区 | 020181 | 防火阀 | 二层西前区 |
| 000286 | 第二分区 | 020182 | 防火阀 | 二层西前区 |
+--------+----------+--------+----------+----------------+