Multiple fields in query don't work in C++ Builder 6

那年仲夏 提交于 2019-12-25 01:08:54

问题


I am using C++ Builder 6 and I want to query more than one field/column in my query to a table of my MySQL database.

Let's say, I have a table named "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 I found in this guide.

Could I query more than 1 field at the same time?

FULL CODE:

String query;

outputMemo->ClearSelection();


// PROBLEMATIC QUERY !!!
query = "SELECT * FROM users;";


try {
  SQLQuery1->SQL->Text = query;
  SQLQuery1->Active = true;
}
catch (Exception& E) {
  outputMemo->Text = "Exception raised with message" + E.Message;
}


// Show the results of the query in a TMemo control.

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.


回答1:


The guide you link to uses SQLite and not MySQL as in your code.

I am suggesting that the files will be different for each DBMS.

Although MySQL can be used directly with it's own drivers employing MySQL's api, using ODBC with MySQL might allow the use of the components.




回答2:


I found that the problem was the wrong dbExpress Driver for MySQL. Dll dbxopenmysql50.dll might work right only with MySQL v5.0. DevArt dbExpress Driver for MySQL could be used for higher versions than v5.0 of MySQL.



来源:https://stackoverflow.com/questions/46076728/multiple-fields-in-query-dont-work-in-c-builder-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!