//这是把文件(文件中内容如:453 323 5 34 3454 #
5654 456 56 453 #)中内容放到数据库里
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "mysql/mysql.h"
FILE * fp;int main()
{   int m_d_add0,m_d_add1,m_d_add2,m_d_add3,m_d_add4,m_d_add5,
      m_s_add0,m_s_add1,m_s_add2,m_s_add3,m_s_add4,m_s_add5,
      m_protocol,
      ip_header_length,ip_tos,ip_total_length,ip_id,ip_off,ip_protocol,
      ip_s_addr0,ip_s_addr1,ip_s_addr2,ip_s_addr3,
      ip_d_addr0,ip_d_addr1,ip_d_addr2,ip_d_addr3,
      udp_source,udp_dest,udp_len;   //文件中一行的内容,从文件里读出一行复值到相应的变量,然后放到数据库中   MYSQL *mysql;
   MYSQL_ROW row;
   char query[2048];
   int res;   mysql=mysql_init(NULL);
   if (!mysql_real_connect(mysql,“localhost”,“root”,“”,“udp”,0,NULL,0))
      {
       printf( "Error connecting to database: %s\n",mysql_error(mysql));
      }  //就是在这里,说不能连接数据库, root是客户端用户名,“”空,为密码
         “udp”是我在mysql中建立的一个表
   else {printf("Connection success\n");}  if((fp=fopen("udp","rb+"))==NULL)
    {printf("不能打开udp文件\n");} //打开文件
  
  do{
        fscanf(fp,"%02x:%02x:%02x:%02x:%02x:%02x %02x:%02x:%02x:%02x:%02x:%02x %d %d %d %d %d %d %d %d:%d:%d:%d %d:%d:%d:%d %d %d %d",&m_d_add0,&m_d_add1,&m_d_add2,&m_d_add3,&m_d_add4,&m_d_add5,&m_s_add0,&m_s_add1,&m_s_add2,&m_s_add3,&m_s_add4,&m_s_add5,&m_protocol,&ip_header_length,&ip_tos,&ip_total_length,&ip_id,&ip_off,&ip_protocol,&ip_s_addr0,&ip_s_addr1,&ip_s_addr2,&ip_s_addr3,&ip_d_addr0,&ip_d_addr1,&ip_d_addr2,&ip_d_addr3,&udp_source,&udp_dest,&udp_len);   //把文件中一行的数都读出来放到相应的变量里
        sprintf(query,"INSERT INTO udp(id,m_d_add0,m_d_add1,m_d_add2,m_d_add3,m_d_add4,m_d_add5,m_s_add0,m_s_add1,m_s_add2,m_s_add3,m_s_add4,m_s_add5,m_protocol,ip_header_length,ip_tos,ip_total_length,ip_id,ip_off,ip_protocol,ip_s_addr0,ip_s_addr1,ip_s_addr2,ip_s_addr3,ip_d_addr0,ip_d_addr1,ip_d_addr2,ip_d_addr3,udp_source,udp_dest,udp_len) VALUES(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", m_d_add0,m_d_add1,m_d_add2,m_d_add3,m_d_add4,m_d_add5,m_s_add0,m_s_add1,m_s_add2,m_s_add3,m_s_add4,m_s_add5,m_protocol,ip_header_length,ip_tos,ip_total_length,ip_id,ip_off,ip_protocol,ip_s_addr0,ip_s_addr1,ip_s_addr2,ip_s_addr3,ip_d_addr0,ip_d_addr1,ip_d_addr2,ip_d_addr3,udp_source,udp_dest,udp_len);   //把插入语句放到一个char型数据        res = mysql_query(mysql, query); //放入数据库        fseek(fp,2,1);    //使指向文件内容的指针指向下一行
     }while(fgetc(fp)!=EOF); //直到文件内容到最后   close(fp);  return 0; 
}
[root@0-8-2-df-fa-ee ~]# ps -a|grep mysqld
系统输出:
26702 pts/1    00:00:00 mysqld_safe
26723 pts/1    00:00:00 mysqld
以上表示确实启动了mysql服务器
然后编译输出写好的C API程序(在write中):
[root@0-8-2-df-fa-ee ~]# gcc write.c -L/usr/lib/mysql -lmysqlclient
[root@0-8-2-df-fa-ee ~]# ./a.out
系统提示:
Error connecting to database: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
为什么不能通过那个sock连接本地主机呢?

解决方案 »

  1.   

    在命令行下执行:
    mysql -u root
    能进去么?
      

  2.   

    将if (!mysql_real_connect(mysql,“localhost”,“root”,“”,“udp”,0,NULL,0))改为if (!mysql_real_connect(&mysql,“localhost”,“root”,“”,“udp”,0,NULL,0))试试,注意有“&”。
      

  3.   

    在命令行下执行:
    mysql -u root
    能进去么?
    如果不行的话,那就over了,是你的参数有问题了