C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 26th, 2002, 06:20 AM
Valeron Valeron is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 Valeron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 !

Reply With Quote
  #2  
Old April 26th, 2002, 07:24 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
i think it should be:

int x=*(int *)row[0]

for the typecast.
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #3  
Old April 28th, 2002, 03:19 AM
Valeron Valeron is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 Valeron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy 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 !!

Reply With Quote
  #4  
Old April 28th, 2002, 04:22 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,966 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 52 m 24 sec
Reputation Power: 189
digging a little on the mysql website:

did you know that there is a c++ class for mysql? there is good step-by-step examples provided on this url:

http://www.mysql.org/documentation/...4_Tutorial.html

this should make it much easier

Reply With Quote
  #5  
Old April 30th, 2002, 07:43 AM
Valeron Valeron is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 Valeron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.





Reply With Quote
  #6  
Old April 30th, 2002, 09:50 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,389 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 17 m 29 sec
Reputation Power: 4080
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?

Reply With Quote
  #7  
Old April 30th, 2002, 10:07 AM
Valeron Valeron is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 Valeron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 !!

Reply With Quote
  #8  
Old April 30th, 2002, 10:13 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,389 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 17 m 29 sec
Reputation Power: 4080
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.

Reply With Quote
  #9  
Old April 30th, 2002, 10:22 AM
Valeron Valeron is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 6 Valeron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 ?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > transferring MYSQL_ROW's row[0] to a string ?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap