Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading