C++ Borland Builder 6 SQL Query
I'm building an app in CppBB6 that reads/writes data from/in a database. From a table called Students I want to extract the Name of a student using the given Index number (both Name and Index are fields in table Studen开发者_开发问答t) and afterwards I want to put that Name in a String or AnsiString variable.
I'm using this query to select the name:
AnsiString query;
Query1->Close();
Query1->UnPrepare();
Query1->SQL->Clear();
query="SELECT Name FROM Students where Index='" + Edit1->Text + "'";
Query1->SQL->Add(query);
Query1->Prepare();
Query1->Open();
and it all checks out fine, but how do I put that Name in a variable? :/
I'd guess something like
Edit1->Text = Query1->FieldByName("Name")->AsString;
or in case you really only have one field and/or know index of the field(s) then
Edit1->Text = Query1->Fields[0]->AsString;
And as Ken White suggest in comments - one should really use parameterized queries for safety and perfomance reasons, something like
Query1->SQL->Add("SELECT Name FROM Students where Index = :idx");
Query1->Prepare();
Query1->ParamByName("idx")->AsString = Edit1->Text;
精彩评论