0

I have 4 mana types lava,water,dark,and Nature. I want to do a query on each one , one at a time. Currently i have

    procedure TFGame.GetStartingCards;
var
   ManaType :string  ;
begin
Manatype := 'Nothing';
    while ManaType <> 'Nature' do
      begin
        if ManaType = 'Dark' then
            ManaType := 'Nature';
        if ManaType = 'Water' then
            ManaType := 'Dark';
        if Manatype = 'Lava' then
            Manatype := 'Water';
        if Manatype = 'Nothing' then
           ManaType := 'Lava' ;
        with adoquery1 do
          begin
             close;
             sql.Clear;
             sql.Add('SELECT * ');
             sql.Add('FROM Cards');
             sql.Add('WHERE Color='''+ManaType+'''');
             open;
          end;

          //return the result of everything for giving mana type..
    end;

but seems to be a better way, like putting each mana type into an array and using the array to feed the query. But could not get it to work. So question is Would this have any flaws if i decided to keep it like this?

4

3 回答 3

8

I would recommend introducing an enumerated type for mana, and maybe a const array of string to use for queries

type 
  TManaType = (mtNothing, mtNature, mtDark, mtWater, mtLava);

const
  cManaQueryNames : array[mtNothing..mtLaval] of string =
    ('nothing','nature','dark','water','lava');

Also, in your database you could consider storing manatype as a number, coresponding to the numerical value (ord) of the TManaType, so that in your database, water mana should be stored as ord(mtWater) = 3

于 2013-10-21T06:51:14.957 回答
1

You could use a single SQL query to get all results for your mana types:

SELECT * FROM cards WHERE Color IN ('Dark', 'Nature', 'Water', 'Lava')

Or pop the types in an array and loop:

for manaType in manaTypes do
begin 
// run an SQL query
end;

The array is the better way, as you can now get the mana types from somewhere else (database, config file), and they are not hard-coded in your source code. This is better because you might change your mind about their names, or their number, and if you do not have to modify and recompile your program in this case, that is more efficient.

于 2013-10-21T06:52:44.953 回答
1
procedure TFGame.GetStartingCards;
const
  ManaTypes : array [0..4] of string = ('Nothing', 'Lava', 'Water', 'Dark', 'Nature');
var
   i: integer;
begin
  for i := 0 to Length(ManaTypes) - 1 do
  begin
    with adoquery1 do
    begin
      close;
      sql.Clear;
      sql.Add('SELECT * ');
      sql.Add('FROM Cards');
      sql.Add('WHERE Color='''+ManaTypes[i]+'''');
      open;
    end;

      //return the result of everything for giving mana type..
  end;
end;
于 2013-10-21T06:59:23.313 回答