mysql - C++ stops connecting to DB after so many successful connects -
i using c++ write directly mysql database, program write first 151 items in data base gets number 152 fails connect data base , throws exact error:
unhandled exception @ 0x6188f1f9 (libmysql.dll) in project1.exe: 0xc0000005: access violation reading location 0x000003b0
i have no idea means , cannot seem find on internet it. below post code writes db. again though working fine until number 152.
void writetodb(string coordstring, string time, string id){ mysql *connect; // create pointer mysql instance connect=mysql_init(null); // initialise instance /* if irrelevant , don't need show it. kept in fault testing.*/ if(!connect) /* if instance didn't initialize , exit fault.*/ { fprintf(stderr,"mysql initialization failed"); } /* connect specific database.*/ connect=mysql_real_connect(connect,server,user,password,database,0,null,0); /* following if statements unneeded too, it's worth show on first app, if database empty or query didn't return @ least let know connection mysql server established. */ if(connect){ printf("connection succeeded\n"); } else{ printf("connection failed!\n"); return; } mysql_res *result; /* create pointer recieve return value.*/ mysql_row row; /* assign variable rows. */ std::string strsql = "insert locationtime (id, datetime, location) values ('" + id + "','" + time + "','" + coordstring + "')";; const char* sql = strsql.c_str(); mysql_query(connect, sql); /* send query database. */ mysql_close(connect); /* close , shutdown */ }
edit: okay able stop throwing error, still mysteriously refusing connect db, did test php script instead , got 800+ values in table no issue @ all. im not sure issue @ all!
if mysql_real_connect()
had failed should put return;
or exit(1)
after printf("connection failed!\n");
:
if(connect) { printf("connection succeeded\n"); } else { printf("connection failed!\n"); return; // <<< }
otherwise program crash, without see error notification printed.
check going wrong mysql_real_connect()
function (and maybe give more useful information "connection failed!\n"
), can use mysql_errno()
function. list of error codes relevant function can found here.
another possibility passing invalid or empty data insert
statements. put integrity checks data passed coordstring, time, id
.
Comments
Post a Comment