The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
transferring MYSQL_ROW's row[0] to a string ?
Discuss transferring MYSQL_ROW's row[0] to a string ? in the C Programming forum on Dev Shed. transferring MYSQL_ROW's row[0] to a string ? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 26th, 2002, 06:20 AM
|
|
Junior Member
|
|
Join Date: Apr 2002
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
transferring MYSQL_ROW's row[0] to a string ?
Hi, i'm doing a program that uses mysql and i'm trying to get the content of the result set from row[x] into a variable (string, int or char ). I need help real bad, cause this problem is bottlenecking my entire program, can't work on much else without clearing this.
tried
int x = (int *)row[x] // doesn't work....
int x = *row[x] // no luck either....
i'm wondering will, string(row[x], 3) work ?
or if it doesn't, anyone knows of another way to do it ?
i need the result of row[x] into the string or array or whatever... but it keep giving me something else...... =(
heartfelt thanks for any replies !
|

April 26th, 2002, 07:24 AM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
|
i think it should be:
int x=*(int *)row[0]
for the typecast.
|

April 28th, 2002, 03:19 AM
|
|
Junior Member
|
|
Join Date: Apr 2002
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
not much luck
Thanks M.Hirsch, but unfortunately, no luck either....
oh yeah... i'm doing c++ in linux.
I'll tell u more so u can have a better idea of what i'm trying to do.
i'm writing a reservation program and i need to perform an if else operation on the number of tickets available, based on the travel details provided by the customer.
it looks something like this.
sprintf(c,"select Seats from Bus where dDate = '%s' and vBusID = '%s' ",strM.c_str(), strN.c_str());
mysql_query(&bus,c);
the values in strM and strN are provided by the customer.
the above query will return the number of Seats available in it's row[0] and i need to perform the following operations on it.
if(row[0] > 0)
reserve(a);
string a = "confirmed";
else
{
cout << "no more tickets available, do u want a wait listed ticket ?";
string a ="waitlisted";
reserve(a);
}
however i can't do the above directly using row[0], therefore i tried retrieve the number of seats left from it into an int variable so i can compare it... but using my previous post's and M.Hirsch's method didn't do the trick....
however string m(row[0],1); works to an extent.
with it i managed to retrieve the first number from row[0]. this way, i managed to get the first, value from row[0]'s data, if it's -11, m stores - , if it's 10 m stores 1 and if it's 0 it'll store 0.
with m, i tried the following codes.
if(m == "0" | m == "-")
cout <<"no tickets";
else
reserve();
however, when i run it, the part where i'm comparing m with - gave me a segmentation fault, without it, the program runs fine. I'm really bummed and don't know how else to do it... the deadline's short and i need to study for exams coming up in a few days time too....
please HELP !!!
thanks a bunch !!
|

April 28th, 2002, 04:22 AM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
|

April 30th, 2002, 07:43 AM
|
|
Junior Member
|
|
Join Date: Apr 2002
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Hi..
M.Hirsch, thanks for the effort.. but seems like my linux server don't have mysqlplus installed... sigh...
previously i said...
if(m == "0" | m == "-")
cout <<"no tickets";
else
reserve();
however, when i run it, the part where i'm comparing m with - gave me a segmentation fault
but when i run it today, hey.. presto ! no more segmentation fault !! but unfortunately, the comparison didn't work either...
but this lead me to another question....
What's the deal with segmentation faults ? I've been getting a lot of NONSENSE segmentation faults....
For instance.. a simple
mysql_close(&mysql);
gave me a segmentation fault a few days ago... after putting the // in front of it the program runs fine... but the next day, i removed the // and strangely, the segmentation fault didn't appear this time around !!
And today, another simple statement...
sprintf(cq,"select * from SeatAvailability where dDate = '%s' ",strDate.c_str()) gave me a segmentation fault !!!
What's going on ?? There's a few other such seg faults that comes and goes... I really don't know what's causing it..
Can u tell me what's going on ?
And is there something wrong with this code ? Since mysqlplus is mostly out of my reach... ( I doubt the not TOO HELPFULL admins in my college will do much with it ) can anyone try this out for me ?
I don't have linux not mysql at home, so i can't work on it.. the time that i do have to work on it in college, i'm attacked by bizzare and unexplainable segmentation faults i told u above...
cout << "n is " << n << "m is " << m << endl; /* Shows 1 and 0 or 1 and - */
if(n == 1 & m == "0" | n == 1 & m == "-")
cout <<"no tickets";
else
reserve();
any ideas why this if statement's not working ?
Even my teachers are baffled with it.... i don't know what to do.

|

April 30th, 2002, 09:50 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
I notice that you're using & and | in all your if statements. These are bitwise operators. Shouldn't you be using && and || (logical AND and OR) instead? You might also want to use extra parenthesis "()" in your if statement as follows:
if ((n==1 && m=="0") || (n ==1 && m=="-"))
When in doubt, use extra parens!
Also for this statement:
sprintf(cq,"select * from SeatAvailability where dDate = '%s' ",strDate.c_str())
Can you show us how you've declared cq and initialized it?
|

April 30th, 2002, 10:07 AM
|
|
Junior Member
|
|
Join Date: Apr 2002
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
okie... here's something
i declared cq as...
char *cq;
cq = new char[333];
there shouldn't be any problems with it eh ?
about the bit wise operator,
yeah... you're correct it should be ||
=)
oh yeah, i just remembered another one of those weird segmentation faults...
after
mysql_query(&me,cq);
delete [] cq; // gave me a segmentation fault too !! made it into a comment and the prob goes away ... any logical explanations ?
thanks !!
|

April 30th, 2002, 10:13 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Re: okie... here's something
Quote: Originally posted by Valeron
i declared cq as...
oh yeah, i just remembered another one of those weird segmentation faults...
after
mysql_query(&me,cq);
delete [] cq; // gave me a segmentation fault too !! made it into a comment and the prob goes away ... any logical explanations ?
|
Well, if your code looks something like this, then you've got a problem:
Code:
char *cq;
cq = new char[333];
while (somecondition) {
...
...
mysql_query(&me, cq);
delete [] cq;
}
This is only a theory, but I suspect that you're deallocating memory somewhere and then referencing it again.
|

April 30th, 2002, 10:22 AM
|
|
Junior Member
|
|
Join Date: Apr 2002
Posts: 6
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
it's not a while....
the cq is only used once in the query, and then it's not used at all, so i deleted it away... it's not in a while or for loop.. it's at the end of the function. so what gives ?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|