0

I am using Connector C to connect to my MySQL database. A modification that I have made to the database recently now allows the data in my url field to be NULL. Connector C does not appear to have any problems reading the NULL value, but when I try and pass the value to my array structure using strcpy, the program crashes. Here is a simplified version of my code:

mysql_real_connect(conn, server,user,password,database, port, NULL, 0);
mysql_query(conn, "SELECT * FROM main WHERE propType IN ('Single Family', 'Condominium')");
res = mysql_use_result(conn);

while (((row = mysql_fetch_row(res)) != NULL) && (row[0] != NULL)) {

    props[count].uniqueID = atol(row[0]);
    strcpy(props[count].address, row[1]);
    .
    .
    .
    strcpy(props[count].url, row[55]);
    count++;
}

By tracing out output of the rows, I have determined that it is this line of code that is failing, and it is ONLY failing when row[55] is (null):

strcpy(props[count].url, row[55]);

I am fairly new to C, and I assume that the problem lies in trying to use strcpy with a null string.

Any suggestions?

sax
  • 337
  • 1
  • 14
  • `strcpy` will crash if one of the pointer is `NULL`. You have to check the pointer by yourself. – Benjamin J. Apr 01 '17 at 20:14
  • What do your structure look like? What is `props[count].address`? Is it an array or a pointer? If it's a pointer, is it properly initialized? – Some programmer dude Apr 01 '17 at 20:20
  • My structure looks like the following: struct columns { long int uniqueID; char address[75]; . char url[400]; }; struct columns props[200000]; – sax Apr 01 '17 at 20:34

1 Answers1

0

As is suggested above in the comment the problem is that row[55] has the value NULL and so strcpy() will crash. Maybe you want to try the following:

if (row[55] != NULL)
    strcpy(props[count].url, row[55]);
else
    props[count].url[0] = '\0';

Here is another example code which use a bit to store if the database contains NULL or a empty value:

if (row[55] != NULL)
{
    strcpy(props[count].url, row[55]);
    props[count].urlEmpty = false;
}
else
{
    props[count].url = '\0';          // Maybe you can skip this
    props[count].urlEmpty = true;
}

In this case you need to expand your structure.

Benjamin J.
  • 1,239
  • 1
  • 15
  • 28
  • I will give this a shot, but it brings up a related issue. I have to write BACK to my database, and if my url value is NULL, I need to write back a null value to my database. Will setting props[count].url = '\0', and then writing this value to my database set my url field to NULL in my database? Is '\0' and NULL synonymous? – sax Apr 01 '17 at 20:52
  • You are right '\0' and NULL is not equal. '\0' represents an empty string and NULL represents an _none exisiting string_ Then maybe you have to hold a separate bit if the value is NULL or not. – Benjamin J. Apr 01 '17 at 21:02
  • OK. I am checking the MySQL documentation for passing a null value to a prepared statement. Your suggestion is likely going to be very helpful, but a little over my head at the moment. Are you suggesting that I set up a Boolean value or something? – sax Apr 01 '17 at 21:11
  • Yes. I would expand your structure by a bool with a name like `urlEmpty` and set it to `true` if the database value is `NULL`. If you want to write it back, you can check this bit and give back either your value or `NULL`. - I have adpated my post above. – Benjamin J. Apr 01 '17 at 21:17
  • Thank you for your help Benjamin! Between your suggestions and a little online documentation, I was able to develop a solution to handle a null string and write a null back to my database. Unbelieveable how little documentation or tutorials exist online to help with C/MySQL interface! – sax Apr 02 '17 at 00:03