Log in

View Full Version : C++ and mysql


Tsholofelo
May 10, 2007, 04:49 AM
I want a query that can select all names in a table and write it in sequential form in a listbox of a form in C++.the connection is well established and communication between my system and my datbase is working fine.

here is the statement I used:

requestName = SELECT SystemName FROM System;
my_query->SQL->Add(requestName);
my_query->Open();
systemRequested = Form1->Bugs_Query->FieldByName("SystemName")->AsString;
listbox1->Items->Add(systemRequested );

it is working fine except that it only gives the first systemName, but I need the whole list. I realised that mysql counts rows from 0 to last-1. I tried for loop but it gives errors:

for(int x=0;x!=-1;x++)
{
requestName = SELECT SystemName FROM System;
my_query->SQL->Add(requestName);
my_query->Open();
systemRequested = Form1->Bugs_Query->FieldByName("SystemName")->AsString;
listbox1->Items->Add(systemRequested );
}

I tried putting vlue of x everywhere it is possible but none seem to work, does anybody know how I can do this.

please help it is very urgent.

thanks (tsholo)

Nosnosna
May 10, 2007, 01:50 PM
You will need to continue using the same connection, query, and reader. In your for() loop, you restart the query each iteration... this method will return the first row every time it's run.

Also, you cannot use control variables the way you have: x will, by inspection, never reach the termination condition without generating an overflow exception.

What method are you using to connect your program to the database? Without knowing the datatypes being used, it's impossible to figure out how to fix it.

Tsholofelo
May 14, 2007, 01:50 AM
actually there is a way of doing it and I did figure it out but anyway thanks for trying to help. Just in case u might need this. A tquery component of C++ has a property RowAffected, al u have 2 do is test if the rows affeted are > 0 then keep on reading for as long as there is something in the database

while( TQuery->RowsAffected > 0 )
{
systemRequested = Form1->TQuery->FieldByName("SystemName")->AsString;
TQuery->Next();





}