简体   繁体   中英

how to cast a mysql pointer to unsigned long

I'm having trouble converting row[1] to an unsigned long in the following program on a 64 bit linux system:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <mysql.h>

MYSQL *conn;

int main()
{

    char query[6500];
    int mysqlStatus = 0;

    MYSQL_RES *result;
    MYSQL_ROW row;
    int num_rows_result = 0;

    unsigned long long_ip;
    int i = 0;

    /* connect to db and initialize arrays with values read from db */
    conn = mysql_init(NULL);
    mysql_real_connect(conn, "host", "username", "passwd", "db", 0, NULL, 0);

    sprintf(query," ");
    sprintf(query,"SELECT * FROM rolla_sorted_in1");
    mysqlStatus = mysql_query(conn, query);

    if (mysqlStatus)
    {
        printf("there was a problem with the following mysql query:\n");
        printf("\t%s\n",query);
    }

    result = mysql_store_result(conn);
    num_rows_result = mysql_num_rows(result);

    if ( num_rows_result > 0 )
    {

        for (i=0;i<10;i++)
        {

            row = mysql_fetch_row(result);

            long_ip = (unsigned long) row[1];
            printf("row[1]: %s, long_ip: %lu\n",row[1],long_ip);

        }

    }
    else
    {
        printf("sorted_in1 is empty\n");

    }

    mysql_free_result(result);

    printf("sizeof(ptr): %lu, sizeof(ul): %lu\n",sizeof(row[1]),sizeof(long_ip));

}

Here is the output:

row[1]: 419670989, long_ip: 6420226
row[1]: 1102313293, long_ip: 6420354
row[1]: 3294303560, long_ip: 6420482
row[1]: 1408935374, long_ip: 6420610
row[1]: 3242924999, long_ip: 6420738
row[1]: 1509939008, long_ip: 6420866
row[1]: 2707912135, long_ip: 6420994
row[1]: 2556917191, long_ip: 6421122
row[1]: 3510964689, long_ip: 6421250
row[1]: 1186041166, long_ip: 6421379
sizeof(ptr): 8, sizeof(ul): 8

Any pointers? Thanks.

A very short answer

Use long_ip = strtoul(row[1], NULL, 0) to get the unsigned long value;

Slightly elaborated

:-)

row[0] .. row[n-1] for n columns are all POINTERS to type-safe field-length determined raw bytes. Integers and floats are NOT stored in machine types for int or longs; but their byte representations ... So for instance 255 is not stored as 0xFF byte it's stored as 2 5 5 : a string of length 3.

The right way (sort of)

You have to use mysql_fetch_field to retrieve the field.... is an array of byte counted strings that may not be NULL terminated because they are binary data. (see MYSQL_ROW) so you have to make sure you use the correct length of bytes using mysql_fetch_lengths(results) for each row and use something like snprintf(field[k], "%.s", (int) lengths[k], row[k]); to put the data into a null terminated string for where the data is NOT A BLOB (for anything that's a blob you have to treat it using memcpy) in your case the data is an integer (which is really stored as a string at row 1 ) so you have to use long_ip = strtoul(row 1 , NULL, 0); to get the unsigned long out.

WHAT YOU REALLY SHOULD READ

MySQL C API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM