Quantcast
Channel: MySQL Forums - Connector/C++
Viewing all articles
Browse latest Browse all 527

Problem using string manipulation functions with MySql. (no replies)

$
0
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.? 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?

Viewing all articles
Browse latest Browse all 527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>