Pobranie z bazy danych jednej wartości i podstawienie do zmiennej

0

Powitać,
Krótkie zwięzłe pytanie. Jak w Delphi 10.3, przy pomocy komponentów ADO, wykonać takie zapytanie:

SELECT pole1 FROM tabela WHERE pole2 = wartosc;

Zapytanie zawsze zwróci jedną wartość i chciałbym tą wartość przypisać do zmiennej w programie. Nawet nie chcę jej wyświetlać.

1
ADOQuery1.SQL.Text:='SELECT pole1 FROM tabela WHERE pole2 = :wartosc';
ADOQuery1.ParamByName('wartosc').AsString:='abc';  //przekazujesz wartość parametru 
ADOQuery1.Open; //wykonujesz zapytanie
zmienna:=ADOQuery1.FieldByName('pole1').AsString;  //pobierasz wartość pola do zmiennej
ADOQuery1.Close;  

a w przypadku gdy zapytanie zwraca więcej niż jeden rekord można odczytać tak

ADOQuery1.SQL.Text:='SELECT pole1 FROM tabela WHERE pole2 = :wartosc';
ADOQuery1.ParamByName('wartosc').AsString:='abc';   
ADOQuery1.Open; 
if ADOQuery1.RecordCount=0 then
ShowMessage('Zapytanie nie zwróciło żadnego rekordu')
else
begin
  ADOQuery1.First;  //ustawiasz się na pierwszym rekordzie
  while not ADOQuery1.EOF do
  begin
    Memo1.Lines.Add(ADOQuery1.FieldByName('pole1').AsString); 
    ADOQuery1.Next;
  end;
end;
ADOQuery1.Close;
1
Paweł Dmitruk napisał(a):
ADOQuery1.SQL.Text:='SELECT pole1 FROM tabela WHERE pole2 = :wartosc';
ADOQuery1.ParamByName('wartosc').AsString:='abc';  //przekazujesz wartość parametru 
ADOQuery1.Open; //wykonujesz zapytanie
zmienna:=ADOQuery1.FieldByName('pole1').AsString;  //pobierasz wartość pola do zmiennej
ADOQuery1.Close;  

a w przypadku gdy zapytanie zwraca więcej niż jeden rekord można odczytać tak

Ale to "tak" z RecordCount nie do końca jest dobrym pomysłem.

ADOQuery1.SQL.Text:='SELECT pole1 FROM tabela WHERE pole2 = :wartosc';
ADOQuery1.ParamByName('wartosc').AsString:='abc';   
ADOQuery1.Open; 
if ADOQuery1.RecordCount=0 then

Nie należy posługiwać się RecordCount w takim przypadku tylko metodą IsEmpty; czasem zawsze będzie tam wartość = 0 lub mniejsza od zera.
Kiedy i jaka, to zależy od komponentów bazodanowych.
Niektóre z komponentów działają tak, że dotknięcie RecordCount spowoduje pobranie wszystkich danych do datasetu.
Inne, potrafią wykonać osobne zapytanie.
A tu chcemy tylko sprawdzić, czy coś zostało zwrócone - dlatego IsEmpty jest lepsze.

ShowMessage('Zapytanie nie zwróciło żadnego rekordu')
else
begin
ADOQuery1.First; //ustawiasz się na pierwszym rekordzie

Wykonanie First bezpośrednio po Open również nie jest potrzebne :-)

  while not ADOQuery1.EOF do
  begin
    Memo1.Lines.Add(ADOQuery1.FieldByName('pole1').AsString); 
    ADOQuery1.Next;
  end;
end;
ADOQuery1.Close;

Nie czepiam się, piszę tylko o tym o czym nie wszyscy mogą wiedzieć.
A warto to wiedzieć i sprawdzić jak się zachowują używane przez nas komponenty Data Access Layer, ponieważ można się zdziwić.

0

There are many stunning apk apps and games that you can get from our Android store. The follows are two of them. Let’s check them out!

**Outlook Mobile
**
Outlook Mobile is an utility app that brings fast connection via email, search and calendar. This app has great integration with Office applications, files and groups to increase your productivity and collaboration.
With Outlook Mobile, the security level of your data will be ensured. This app has many useful functions, for example, built-in device management to protect your information.

Outlook Mobile

Go to the best android market to download apks for free

This app also helps keep important people in the most prominent position. It displays necessary information so that you can get things logically done.
Outlook uses AI to predict your needs, help you maintain your scientific arrangements and plans. With this app, you will easily search files, contacts and emails. The smart calendar will remind you about the time attending meetings or carrying out the activities.
Outlook mobile works well with existing Office 365 on your Android devices. The "add-on" feature on Outlook has been available for Android devices. It allows you to use features from applications such as Trello, Evernote or services like GIPHY directly from the Outlook application.

Features:
User-friendly interface, easy to use.
Easy protection and management
Effectively connecting people
A way to work smarter
Now, let’s move to our game!

**Farm heroes Saga
**
Farm heroes Saga

Download Android apps free on Apkafe app store

Farm Heroes Saga is an intellectual puzzle game. This attractive and free game is now available on Android devices.
With Farm Heroes Saga, you will feel as if you were lost in a world of grass and flowers, fresh food and aroma, which brings endless relaxation.
Getting started, you will play a role as a farmer who owns a colorful farm. Your companions are a cute pig wearing sunglasses and a lovely dog. Meanwhile, Rancid is an evil fox that looks for a way to devastate your farm. Stop him by getting through the levels.
In the game, your task is to match a line of three same-kind items. Like Diamond or Candy Crush, you need to connect three fruits of the same color/type to make them disappear and ta-da, get the score.
Like many other puzzle games, its gameplay is built on the basis of the Match-3 game genre. You must arrange so that at least 3 of the same images are on a vertical or horizontal row to harvest them within limited moves.
Farm Heroes Saga requires you to perform different tasks at a higher level. There are many types of fruit to match such as strawberries, onions, carrots, sun, raindrops, etc.

Features:
The story is simple and close to the farm, the animals, the livestock
The game is suitable for people of all ages
Unlimited connectivity thanks to the ability to synchronize between different devices and platforms
Besides the game and app mentioned above, there are more for you to get from our apk store. Try visiting it and you won’t be disappointed with many wonderful apk apps and games like these. Have fun!

1 użytkowników online, w tym zalogowanych: 0, gości: 1