Code::Blocks returns -10737741819 (0xC0000005) when executing MySQL loop insert c++

Advertisements

I’ve been making program that need to continuously insert data to a database. I’m new to C++.
I’m using xampp for my database. I want to make insert loop inside one of my function.
my code looks like this

#include "stdio.h"
#include "fstream"
#include "iostream"
#include "mysql.h"
#include "sstream"

void loop();
void print();

int i;
const char* hostname    = "localhost";
const char* username    = "root";
const char* password    = "";
const char* database    = "testinsertdb";
unsigned int port       = 3306;
const char* unixsocket  = NULL;
unsigned long clientflag = 0;


insertion(){
    MYSQL* conn;
    conn = mysql_init(0);
    conn = mysql_real_connect(conn, hostname, username, password, database, port, unixsocket, clientflag);
    int qstate=0;
    using namespace std;
    stringstream ss;
        ss << " INSERT INTO test (number) values ('" <<i<<"')";
        string query = ss.str ();
        const char * q = query.c_str();
        qstate = mysql_query(conn, q);
         if (qstate == 0)
        {

            cout <<" Record inserted successfully ..."<<endl;
        }
        else
        {
            cout <<" Error, data not inserted..."<<endl;
        }
}

int main()
{
    print();
    return 0;
}

void print()
{
    for (int j = 0; j < 1000000; j++) {
     loop();
}
}


void loop()
{
    i=1;
    insertion();
}

When I run the program, I managed to insert some data to the database, but after several seconds the program stopped with code -10737741819 (0xC0000005). On my build log Process the terminated with status -1073741510
How can i solve this?

>Solution :

Preferablly try this one.

Your code is trying to connect database as many times as the loop proceeds.

There is the description of that error from this link

#include "stdio.h"
#include "fstream"
#include "iostream"
#include "mysql.h"
#include "sstream"

void loop();
void print();

MYSQL* conn;

const char* hostname    = "localhost";
const char* username    = "root";
const char* password    = "";
const char* database    = "testinsertdb";
unsigned int port       = 3306;
const char* unixsocket  = NULL;
unsigned long clientflag = 0;


void insertion() {
    int qstate=0, i;
    using namespace std;
    stringstream ss;
    
    ss << " INSERT INTO test (number) values ('" <<i<<"')";
    string query = ss.str ();
    const char * q = query.c_str();
    qstate = mysql_query(conn, q);

    if (qstate == 0)
    {

        cout <<" Record inserted successfully ..."<<endl;
    }
    else
    {
        cout <<" Error, data not inserted..."<<endl;
    }
}

int main()
{
    print();
    return 0;
}

void print()
{
    conn = mysql_init(0);
    conn = mysql_real_connect(conn, hostname, username, password, database, port, unixsocket, clientflag);

    for (int j = 0; j < 1000000; j++) {
     loop();

    mysql_close();     
}



void loop()
{
    i=1;
    insertion();
}

Leave a ReplyCancel reply