MySQL Forums
Forum List  »  Connector/C++

Problem using string manipulation functions with MySql.
Posted by: Brian Vogl
Date: November 01, 2019 09:28AM

// 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.? I'm
// experimenting with using prepared 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...

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

//Now why is that happening?

Options: ReplyQuote


Subject
Views
Written By
Posted
Problem using string manipulation functions with MySql.
545
November 01, 2019 09:28AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.