سؤال

I have an sqlite database and i want to execute a select query and show the result in the TEdit. How can i do it?

 query := 'SELECT username FROM users';  //The query returns only one row
 FDQuery1.ExecSQL;
 FDQuery1.Open();
 Edit1.Text := ??
هل كانت مفيدة؟

المحلول 2

In your case, because you only have 1 column I would use:

Edit1.Text := FDQuery1.Fields[0].AsString;

But if you have mulitple column you select I would use:

Edit1.Text := FDQuery1.fieldbyname(<ColumnName>).AsString;

نصائح أخرى

Edit1.Text := FDQuery1.Fields[0].AsString;

Please note that ExecSQL executes an SQL statement that does not return data, while Open executes a SELECT query. So you are executing the query twice.

ExecSQL haven't return value

Examp:

 FDQuery1.sql.add:= 'SELECT username FROM users';
 FDQuery1.Open();

 Edit1.Text := FieldByName('username').AsString;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top