I am using C ++ Builder 6 and I want to query more than one field / column in my query on the table of my MySQL database.
Let's say I have a table called "users" and my table has the fields "id", "name", "username" and "password". Please check the following examples:
query = "SELECT name FROM users;"; // WORKS
query = "SELECT name, username FROM users;"; // WORKS ONLY FOR THE 1ST FIELD "name"
query = "SELECT * FROM users;"; // DOESN'T WORK: GIVES ME EAccessViolation
query = "SELECT name FROM users UNION SELECT username FROM users;"; // WORKS BUT IT ISN'T A SOLUTION
So far, the rest of my code is almost the same as in this guide .
Can I request more than one field at a time?
FULL CODE:
String query;
outputMemo->ClearSelection();
query = "SELECT * FROM users;";
try {
SQLQuery1->SQL->Text = query;
SQLQuery1->Active = true;
}
catch (Exception& E) {
outputMemo->Text = "Exception raised with message" + E.Message;
}
TStringList *list;
TField *currentField;
String currentLine;
if (!SQLQuery1->IsEmpty()) {
SQLQuery1->First();
list = new TStringList;
__try {
SQLQuery1->GetFieldNames(list);
while (!SQLQuery1->Eof) {
currentLine = "";
for (int i=0; i<list->Count; i++) {
currentField = SQLQuery1->FieldByName(list->Strings[i]);
currentLine = currentLine + " " + currentField->AsString;
}
outputMemo->Lines->Add( currentLine.c_str() );
SQLQuery1->Next();
}
}
catch (Exception& E) {
outputMemo->Text = "Exception raised with message" + E.Message;
}
list->Free();
}
Thank you in advance for your help and your time.
source
share