473,394 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

prolem in calling mysql C API in C++ project

Hi all,

I am using mysql C API in my C++ project. I am using
mysql-4.1.4-gamma and gcc 3.2.2.

Problem with this code is that when add_data(i) is called second time
SEGMENTATION FAULT occurs at mysql_real_query(). First time it
executes the add_data() function successfully.
My Code is

MYSQL db is declared in main.cpp and database connection is also made
in main.cpp.

//main.cpp
add_carrier();

//carrier.cpp
add_carrier()
{
for(i=0;i<3;i++)
{
add_data(i);
}
}
//country.cpp
extern MYSQL db;
add_data(int i)
{
char sqlquery[500];
sprintf(sqlquery,"Select rateid from RATES where carrierid=%d",i);

//*** CAUSES SEGMENTATION FAULT WHEN add_data() IS CALLED SECOND
TIME
mysql_real_query(&db,sqlquery,strlen(sqlquery)); //SEGMENTATION
FAULT
...
...
...

}

Thanx in advance.

Regards,
Manpreet Dhanjal
Jul 20 '05 #1
1 1853
[answering in comp.lang.c++]
Manpreet wrote:
I am using mysql C API in my C++ project. I am using
mysql-4.1.4-gamma and gcc 3.2.2.

Problem with this code is that when add_data(i) is called second time
SEGMENTATION FAULT occurs at mysql_real_query(). First time it
executes the add_data() function successfully.
My Code is

MYSQL db is declared in main.cpp and database connection is also made
in main.cpp.

//main.cpp
add_carrier();

//carrier.cpp
add_carrier()
{
for(i=0;i<3;i++)
{
add_data(i);
}
}
//country.cpp
extern MYSQL db;
add_data(int i)
{
char sqlquery[500];
sprintf(sqlquery,"Select rateid from RATES where carrierid=%d",i);

//*** CAUSES SEGMENTATION FAULT WHEN add_data() IS CALLED SECOND
TIME
mysql_real_query(&db,sqlquery,strlen(sqlquery)); //SEGMENTATION
FAULT
...
...
...

}

The code you posted is neither C nor C++. If I add all the necessary
elements to make it compilable, it still contains plenty of "..." which
doesn't help.

Segmentation faults are usually due to accessing memory using invalid
pointers. Either some pointer gets stumped over, or you cross some array
boundary, or both. In the presented code there is no problem, so I say
it's somewhere in the code you didn't post.

Run your program under a debugger and examine the cause of the seg fault.

V
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: user mysql | last post by:
HELLO FRIENDS. HERE A FANTASTIC NEWS FOR MYSQL WINDOWS USER. READE THIS. The article is grab from www/internetnews/com/ DO YOU THINK THAT IS A GOOD NEWS ? ...
0
by: Rich R | last post by:
First, let me introduce myself to the group. My name is Rich and I've been doing databases for about 25 years with the last 10 being pretty much MS SQL Server. I've become intrigued with MySQL and...
17
by: chicha | last post by:
Hey people, I have to convert MS Access 2000 database into mysql database, the whole thing being part of this project I'm doing for one of my faculty classes. My professor somehow presumed I...
1
by: Manpreet | last post by:
Hi all, I am using mysql C API in my C++ project. I am using mysql-4.1.4-gamma and gcc 3.2.2. Problem with this code is that when add_data(i) is called second time SEGMENTATION FAULT occurs...
175
by: Sai Hertz And Control Systems | last post by:
Dear all, Their was a huge rore about MySQL recently for something in java functions now theirs one more http://www.mysql.com/doc/en/News-5.0.x.html Does this concern anyone. What I...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
8
by: Darwiniv | last post by:
1. Does anyone run ASAPI and mysql succesfully on Vista /IIS7? 2. Does anyone run CGI and display all SESSION errors on Vista /IIS7? php ASAPI doesn't works with MYSQL. And CGI doesn't show...
1
by: bylum | last post by:
prolem exception org.apache.jasper.JasperException at jsp the list exception : org.apache.jasper.JasperException...
4
by: vijjibalu | last post by:
Hi all, as part of my project requirement we are migrating procedures from mssql to mysql below is the code in mssql CREATE PROCEDURE TABLE_DEPEND_INSERT_ORDER AS CREATE TABLE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.