#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    6
    Rep 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 !
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    i think it should be:

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

    for the typecast.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    6
    Rep 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 !!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    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/m..._Tutorial.html

    this should make it much easier ;)
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    6
    Rep 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.




    :(
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    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?
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    6
    Rep 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 !!
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207

    Re: okie... here's something


    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.
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    6
    Rep 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 ?

IMN logo majestic logo threadwatch logo seochat tools logo