samedi 1 août 2015

Not able to insert values in mysql using C and Linux

I have created a client server program and using sql in it for the first time. I am getting issues in the sql. to create table i have used the following command

char nsds_table_cmd[]="DROP TABLE IF EXISTS NSDS_TABLE; CREATE TABLE NSDS_TABLE(ID BIGINT, nguid VARCHAR(16),MAC CHARACTER(6), MACHINE_TYPE SMALLINT, STATE SMALLINT, \
                         REGISTERED_HOST TEXT, TIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY(ID,MACHINE_TYPE))";
char nsds_database_cmd[]="DROP DATABASE IF EXISTS nsdsDB; CREATE DATABASE nsdsDB";

I have created a struct whose object i am passing to my function that insert into the table

typedef struct nsds_entry {
        long int client_id;
        char nguid[nguid_len];
        char mac[mac_len];
#if 0
        char function_id;
        unsigned int controller_id;
#endif 
        short int client_type;
        short int current_state;
        long int registered_host;
}__attribute__ ((packed)) sql_cmd;

In server when i receive a packet from the client which have the following values

 Destination_address = FF-FF-FF-FF-FF-FF 
    Source_address      = 00-00-00-00-00-00
    Ether_type          = 9998
    Packet_type         = 3
    ACK_type            = 0
    Client_type         = 2
    Client_mac          = 00-00-00-00-00-00 
    Server_mac          = 00-00-00-00-00-00 
    HSID                = 01-02-03-04-AB-CD-EF-AC
    Client_nguid        = 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
    EOPS                = ffff0000

i am filling the nsds_entry struct like this

   LOG_INFO("Ack packet transmission successful\n");
    memcpy(g_sql_obj.client_id,ack->client_id,NVMAR_HSID_LEN);
    memcpy(&g_sql_obj.mac,ack->client_mac, NVMAR_MAC_LEN);
    LOG_INFO("ack_tx client_mac = %s\n",g_sql_obj.mac); // this dosen't print i mean no value as shown in output below
    LOG_INFO("ack_tx:   Client_mac        = %.2X-%.2X-%.2X-%.2X-%.2X-%.2X\n", g_sql_obj.mac[0], g_sql_obj.mac[1],
                                                    g_sql_obj.mac[2], g_sql_obj.mac[3], g_sql_obj.mac[4], g_sql_obj.mac[5]); // this prints exactly the value we copied
    memcpy(g_sql_obj.nguid,ack->client_nguid,NVMAR_NGUID_LEN);
    LOG_INFO("ack_tx:   Client_nguid        = %.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\n", g_sql_obj.nguid[0], g_sql_obj.nguid[1],
                                                    g_sql_obj.nguid[2], g_sql_obj.nguid[3], g_sql_obj.nguid[4], g_sql_obj.nguid[5],
                                                    g_sql_obj.nguid[6], g_sql_obj.nguid[7], g_sql_obj.nguid[8], g_sql_obj.nguid[9],
                                                    g_sql_obj.nguid[10], g_sql_obj.nguid[11], g_sql_obj.nguid[12], g_sql_obj.nguid[13],
                                                    g_sql_obj.nguid[14], g_sql_obj.nguid[15]);


    g_sql_obj.client_type = htons(ack->client_type);
    g_sql_obj.current_state = NVMAR_STATE_ACTIVE;
    g_sql_obj.registered_host = 0xffffffff;


 nvmar_db_sql_insert_into_table(&g_sql_obj);


Output are as follows

ack_tx:832:ack_tx:   Client_mac        =
ack_tx:832:ack_tx:   Client_mac        = 00-00-00-00-00-00
ack_tx:838:ack_tx:   Client_nguid        = 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00


unsigned int nvmar_db_sql_insert_into_table(struct nsds_entry *row)
{

    char cmd[buffer_l];

    sprintf(cmd,"%s%ld,'%s','%s',%x,%x,'%lx,',%s","INSERT INTO NSDS_TABLE VALUES(",row->client_id,row->nguid,row->mac,row->client_type,row->current_state,row->registered_host,"NULL)");//original

        if (mysql_query(g_db_ctxt.db_handle.nsds, cmd)) {
        finish_with_error(g_db_ctxt.db_handle.nsds);
    }

    return 1;
}

What i need is when i use the following command in sql "select * from table_name" client_mac should be seen as 00-00-00-00-00-00 not in hex, i mean all value should be seen as above

I know that in creating cmd i am not getting any value for the mac and nguid fields as i am reading them through %s but what is the solution. Do i have to change my table definition?

Aucun commentaire:

Enregistrer un commentaire