473,387 Members | 1,542 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,387 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 22 '05 #1
1 1785
[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 22 '05 #2

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

Similar topics

0
by: Creativy, writing and more | last post by:
Hi all, I'm hoping to find some additional help with a site I'm maintaining for my landlord. I'm trying to grow a web hosting and web design business. I was to have this site as a job that would...
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...
2
by: lawrence | last post by:
We've developed a content management system that we donate to the public domain (via a Creative Commons declaration) at www.publicdomainsoftware.org. Our cms has an emphasis on weblogs, though we...
3
by: Shiraz | last post by:
Updated to the latest version of DBD-mysql using perl -MCPAN -e "install DBD-mysql" and now the calling mysql function r2() within perl work > $SQL_Text = "select r2() from dual " ; >...
3
by: mcena | last post by:
Hi guys. I am developing this project where I ve been forced to include (add) another vb project. Problem is I cant move execution/control from the main project to the new/other project. Please,...
6
by: TS | last post by:
I cannot get this to work. I added an app.config to a project i reference from my web application project (vs 05) but can see no way to access the settings within it. the other thing is that I...
8
by: The Natural Philosopher | last post by:
This is so weird. What I am trying to do is to upload files and stuff them in a mysql database. Everything works except the file content is zero. using the load_file command from mysql...
3
by: Gilles Ganault | last post by:
Hello A friend of mine is running a site that has about 1,000 concurrent users when it's really busy. Until now, he kept data in sessions (including output from SELECT fetched from a remote...
2
by: Jivanmukta | last post by:
Hello, I have little experience in PHP+MySQL programming. I need your help in such problem: how to call stored MySQL function from PHP5 code using mysql_ interface? I failed to find solution in...
5
by: Urmi Biswas | last post by:
Hi All, Has anyone tried calling MySQL stored procedure from php scripts? I use PEAR DB module to access MySQL database. Not sure if php 4.4.0 (that we use) support calling stored procedures. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.