Jay Sprenkle <[EMAIL PROTECTED]> writes:

> On 7/12/05, Gotzon Astondoa <[EMAIL PROTECTED]> wrote:
>>         ...
>>         struct hostent *hep;
>>         ...
>>         hep=gethostbyname(DomainTxt);
>>         ...
>>      strcpy(txtIP, inet_ntoa(*((struct in_addr *)hep->h_addr_list[i])));
>>      ip = inet_addr(txtIP);
>>      ip = ntohl(ip);
>>      strcpy(sql,"INSERT into domains (domain) VALUES ('");
>>      sprintf(nIP,"%u",ip);
>>      strcat(sql,nIP);
>>      strcat(sql, "')");
>
> Try this:
>
> sprintf( sql, "INSERT into domains (domain) VALUES ('%u') ", ip  );

Being pedantic...

  char sql[MAX_QUERY_LEN];
  snprintf(sql, sizeof(sql),
           "INSERT into domains (domain) VALUES ('%lu') ",
           ip);

or

  char * sql;
  if ((sql = sqlite3_mprintf("INSERT into domains (domain) VALUES ('%lu') ",
                             ip)) == NULL)
  {
      fatal("out of memory");
  }

  /* use sql pointer (issue query) */

  sqlite3_free(sql);

Note 1: snprintf() instead of sprintf() to ensure that you don't overrun the
sql buffer.  This function takes the size of the buffer as the second
parameter.  Alternatively, use sqlite3_mprintf() to allocate a buffer of an
appropriate length (containing the query), and then free it when done with the
query.

Note 2: Since you stated explicitely that you were treating the IP address as
a "long", then "%lu" instead of "%u" allows for the possibility that "int" and
"long" are different sizes, which of course, depends on the compiler you're
using.  Using "%lu" is more technically correct if "ip" is declared as "long".


The question remains, though as to why you are using LENGTH() on a numeric
field.  LENGTH() is intended for strings, and presumably type-casts the
numeric value to a string when you call it.


Cheers,

Derrell

Reply via email to