Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2004
    Location
    NJ
    Posts
    37

    Unanswered: couple questions on calling mysql from a c program

    I installed mysql and set up a database "Bandwidthstats" and set up a table "statistics" I also created a user group16 with a password to connect to it. When i do a describe on the table "statistics" i get the following columns: Time, SourceIP, DestIP, Protocol, Port, and Packetlength. Currently I have a C program (tcpdfilter) that it piped through tcpdump to collect some network statistics.

    I have played around with trying to set up the ODBC connection but is this needed if i plan on running the database and sniffer on the same host? I am just trying to find the easiest way to connect to the database through the c program. Meaning that the c program currently creates strings such as IPsource, IPdest, portnumber... etc. I would like to either have mysql calls in the function that prints these fields or pass these fields to another function like CtoSQL() to input the data from there. Is this possible? Can i just say CtoSQL(IPsource, IPdest). I am not very familiar with C and how all that stuff works. I found some examples about this but they haven't been that helpful to me so far. Also i was thinking about creating a separate c program to do this, rather than using the one that i found, but i am new makefiles and the like and i can get the c program i found working.

    All i want to do is take the stated fields from tcpdfilter.c and somehow push them to my mysql database...Can anyone give me some advise?

  2. #2
    Join Date
    Feb 2004
    Location
    NJ
    Posts
    37

    couple more issues

    I have continued to work on this and this is where i stand right now. I editted the makefile that the c program that i have uses to include mysql.h.

    I added:
    #include "/usr/local/mysql/include/mysql/mysql.h"
    to the C program. I did a make on the c program and it was error free. I created my user using the following command:
    mysql> grant all on Bandwidthstats.* to group16@localhost identified by 'group16';

    I looked at various websites on how to connect to my SQL database through C and this is where i am right now with my C program..
    i added:
    -------------------------------------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "/usr/local/mysql/include/mysql/mysql.h>

    #define host "localhost"
    #define username "group16"
    #define password "group16"
    #define database "Bandwidthstats"
    .
    .
    .
    Inside one of my functions i tried to connect to the database:
    MYSQL *mysql;
    mysql_init(mysql);

    if(!mysql_real_connect(mysql,host,username,passwor d,database,0,NULL,0))
    {
    printf("Failed to connect to database: Error: %s\n" , sysql_error(mysql));
    }

    mysql_close(mysql);
    .
    .
    .

    ----------------------------------
    When i make the c program I get the following errors:
    [root@sendlab26 tcpdfilter_2.0]# make
    gcc tcpdfilter.c -Wall -o tcpdfilter # compile output executeable to ./
    /tmp/ccCDxUvp.o(.text+0xaa8): In function `displayHeader':
    : undefined reference to `mysql_init'
    /tmp/ccCDxUvp.o(.text+0xad0): In function `displayHeader':
    : undefined reference to `mysql_real_connect'
    /tmp/ccCDxUvp.o(.text+0xae8): In function `displayHeader':
    : undefined reference to `mysql_error'
    /tmp/ccCDxUvp.o(.text+0xb07): In function `displayHeader':
    : undefined reference to `mysql_close'
    collect2: ld returned 1 exit status
    make: *** [tcpdfilter] Error 1

    Can anyone tell me what i am doing wrong or what i have to do to get C to recognize the mysql statements??

    Any help would be appreciated

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •