小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

一個(gè)將文件數(shù)據(jù)(漢字)導(dǎo)入到mysql的C程序_原創(chuàng)文檔_論壇_PHPChina 開源社區(qū)...

 larrin 2007-05-06

發(fā)表時(shí)間: 2006-12-17 16:45    作者: llehotnwod    來(lái)源: 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中實(shí)在是不會(huì),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  光電感煙    一層?xùn)|后區(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 | 光電感煙 | 二層?xùn)|后區(qū)     |
| 000002 | 第一分區(qū) | 001002 | 光電感煙 | 二層?xùn)|后區(qū)     |
| 000003 | 第一分區(qū) | 001003 | 光電感煙 | 二層?xùn)|后區(qū)     |
| 000004 | 第一分區(qū) | 001004 | 光電感煙 | 二層?xùn)|后區(qū)     |
| 000005 | 第一分區(qū) | 001005 | 光電感煙 | 二層?xùn)|后區(qū)     |
| 000006 | 第一分區(qū) | 001006 | 光電感煙 | 二層?xùn)|后區(qū)     |
| 000007 | 第一分區(qū) | 001007 | 光電感煙 | 二層?xùn)|后區(qū)     |
| 000008 | 第一分區(qū) | 001008 | 光電感煙 | 二層?xùn)|后區(qū)     |
| 000009 | 第一分區(qū) | 001009 | 光電感煙 | 二層?xùn)|后區(qū)     |
.................................................................
| 000285 | 第二分區(qū) | 020181 | 防火閥   | 二層西前區(qū)     |
| 000286 | 第二分區(qū) | 020182 | 防火閥   | 二層西前區(qū)     |
+--------+----------+--------+----------+----------------+

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多