What is the problem using string manipulation functions with MySql?

0

I have a MySQL 8.0.17 database sample project I am compiling using Visual Studio CE 2017 on a 64 bit windows machine. It is a console app. See the code below it's boilerplate. The issue I have is when trying to manipulate strings after initializing the database. ie: uncommenting the sprintf_s() causes an exception to be thrown. I am trying to understand if this is somehow by design or is it a bug? Or is there something else I may be doing wrong?

include "pch.h"
include <windows.h> 
include <iostream> 
include <winsock.h> 
include <stdio.h> 
include <mysql.h>

int main()
{   std::cout << "Hello World!\n";
    MYSQL conn;
    MYSQL_RES *res_set;
    MYSQL_ROW row;

    mysql_init(&conn);

    char str[6];
 // sprintf_s(str, 5, "test");

    if(!mysql_real_connect(&conn,"localhost","usr","pas","db",0,NULL,0))
    {   fprintf(stderr, "Connection Failed: %s\n", mysql_error(&conn));
    } else
    {   fprintf(stderr, "Successfully connected to Database.\n");
        int status = mysql_query(&conn, "SELECT * FROM users");
        res_set = mysql_store_result(&conn);
        int count = mysql_num_rows(res_set);
        printf("No of rows = %d\n", count);
        while ((row = mysql_fetch_row(res_set)) != NULL)
        {   for (int i = 0; i < mysql_num_fields(res_set); i++)
            {   printf("%s \t", row != NULL ? row : "NULL");
            } printf("\n");
        }
    }
    mysql_close(&conn);
 // getchar();
    return 0;
}

I find it interesting that if I place the sprintf_s() above the mysql_init() it works just fine? So does that mean I need to open a new connection on each request? Because it did not work this way in version 5.7 where I could establish a connection then execute several statements. Any response would be greatly appreciated as I've been struggling with this for a while now...

The following is the Exception being thrown by the debugger at the call to mysql_num_rows() when the sprintf_s() is included in the compilation...

Exception thrown at 0x00007FFED78550C0 (libmysql.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

So, I stripped it down to as few lines as possible to reproduce the error:

int main()
{   MYSQL conn;
    MYSQL_RES *res_set;

    mysql_init(&conn);

    char str[50];
    strcpy_s(str, "test");

    if (mysql_real_connect(&conn,"localhost","user","pass","db",0,NULL,0))
    {   int status = mysql_query(&conn, "SELECT * FROM users");
        res_set = mysql_store_result(&conn);
        int count = mysql_num_rows(res_set);  // Exception is thrown here.
    }
    mysql_close(&conn);
}

Now why does this happen? Thanks, Brian..

c++
mysql
windows
64-bit
asked on Stack Overflow Nov 1, 2019 by Snackerdog • edited Nov 2, 2019 by Snackerdog

2 Answers

0

Your are not checking if any of the rows fields are NULL

Use instead

while ((row = mysql_fetch_row(res_set)) != NULL)
{ for (int i = 0; i < mysql_num_fields(res_set); i++)
  {
    printf("%s ", row[i] ? row[i] : "NULL");  
  } 
  printf("\n");
}
answered on Stack Overflow Nov 1, 2019 by nbk
0

Thank you to those who answered my question. I rolled back to version 5.7 of MySQL and it is working correctly. Perhaps I tried to adopt the newer cutting edge version 8.0 a little too early.?

answered on Stack Overflow Nov 4, 2019 by Snackerdog

User contributions licensed under CC BY-SA 3.0