:~> 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 textmain.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²¿·ÖÎÒ²»ÖªµÀÔõôд,Èç¹ûÈ¥µôÄDz¿·Ö,
³ÌÐò¿ÉÒÔÔËÐÐ,µ«²åÔÚzy
Êý¾Ý¿âÖеĺº×Ö²¿·ÖÈ«ÊÇÂÒÂë

cry:cry