Entwickler-Ecke

Datenbanken - SourceCode Problem


mettie84 - Do 21.10.04 09:33
Titel: SourceCode Problem
Also ich poste hier mal die wichtigsten Teile meines Sourcecodes. Ich find einfach den Fehler nicht. Vllt könnt Ihr mir da helfen.


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
const
  cRDefault = 'SELECT';
  cRUsage = 'SELECT, INSERT, UPDATE, DELETE';
  cRMainUsage = 'USAGE';
  cRAdmin = 'ALL PRIVILEGES';

function MakeRights(SecurityLevel: integer; var vRGrant: ShortStr): ShortStr;
function DBChanges(Connection: TZConnection; SQLString: string): boolean;

implementation

function MakeRights(SecurityLevel: integer; var vRGrant: ShortStr): ShortStr;
begin
  vRGrant := ' ';
  case SecurityLevel of
    0: Result := cRUsage;
    1: Result := cRMainUsage;
    2begin
         Result := cRAdmin;
         vRGrant := ' WITH GRANT OPTION';
       end
  end;
end;

function DBChanges(Connection: TZConnection; SQLString: string): boolean;
var Query: TZQuery;
begin
  Query := TZQuery.Create(Application);

  Query.Connection := Connection;
  Query.SQL.Clear;
  Query.SQL.Add(SQLString);

  try
    Query.ExecSQL;
    Result := TRUE;
  except
    Result := FALSE;
  end;

  Query.Free;
end;


//....... Anderer Unit........


procedure TfrmMain_frameAdmin.btnCreateUserClick(Sender: TObject);
var SQLString: string;
    RGrant, Right: ShortStr;
begin
  Right := MakeRights(rgRights.ItemIndex, RGrant);

  if (edtUser.Text <> '')then
  begin
    if ((edtPwd1.Text <> ''and not(edtPwd1.Text <> edtPwd2.Text)) then
    begin
      SQLString := 'GRANT '+ Right + ' ON rma.* TO '''+edtUser.Text+'''@''%'' IDENTIFIED BY ''' + edtPwd1.Text +''''+ RGrant;
    end
    else if ((edtPwd1.Text = ''and (edtPwd2.Text = '')) then
    begin
      SQLString := 'GRANT '+ Right + ' ON rma.* TO '''+edtUser.Text+'''@''%' + RGrant;
    end
    else
    begin
      MessageDlg('Die Passwörter sind nicht identisch', mtError, [mbOk],0);
      Exit;
    end;
  end
  else
  begin
    MessageDlg('Sie haben keinen Benutzernamen eingegeben!', mtError, [mbOk],0);
    Exit;
  end;

  if not DBChanges(SQLConnection, SQLString) then
    MessageDlg('Beim Anlegen des Benutzers "'+edtUser.Text+'" ist ein SQL-Fehler aufgetreten.', mtError, [mbOK], 0);


Die Fehlermeldung:
Sinngemäß: ...check your SQL manual for the right syntax to use near "%' at line 1. Prozess....


smiegel - Do 21.10.04 09:55

Hallo,

ich gehe einmal davon aus, dass es eine MySQL-DB ist. Das Problem könnte im Zusammenbau der Rechte liegen. Anscheinend hast Du zu wenig oder zu viel Anführungszeichen.

Versuche es einmal folgendermaßen:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
  if ((edtPwd1.Text <> ''and not(edtPwd1.Text <> edtPwd2.Text)) then  
  begin  
    SQLString:='GRANT %s ON rma.* TO %s@%% IDENTIFIED BY "%s" %s';  
    SQLString:=Format(SQLString, [Right, edtUser.Text, edtPwd1.Text, RGrant]);
  end  
  else
  begin  
    SQLString:='GRANT %s ON rma.* TO %s@%% %s';  
    SQLString:=Format(SQLString, [Right, edtUser.Text, RGrant]);
  end  
  else  
  ...


mettie84 - Do 21.10.04 10:05

Habs aus probiert. Jetzt sieht die Fehlermeldung so aus:

...to use near '%' at line 1.... :arrow:


smiegel - Do 21.10.04 10:11

Hallo,

hast Du schon einmal überprüft, was im SQLString steht? Hast Du auch in der Format-Anweisung nach dem "@" die zwei %-Zeichen so übernommen? Dies muss so sein, damit die Funktion weiss, dass an dieser Stelle ein %-Zeichen kommt.


mettie84 - Do 21.10.04 10:22

mhh ich weiß jetzt wieder, warum ich die funktion meide. Weil ich sie nicht verstehe. könntest du mir helfen, wie ich ihm sage das das % an der stelle kommt. das %s habe ich verstanden.


MfG Matthias


smiegel - Do 21.10.04 10:31

Hallo,

ich habe mir gerade noch einmal die genaue Syntax von GRANT angeschaut. Da ist mir vorher ein kleiner Fehler unterlaufen. Nachfolgend die korrigierte Variante:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
  ... 
  if ((edtPwd1.Text <> ''and not(edtPwd1.Text <> edtPwd2.Text)) then    
  begin    
    SQLString:='GRANT %s ON rma.* TO %s@"%%" IDENTIFIED BY "%s" %s';    
    SQLString:=Format(SQLString, [Right, edtUser.Text, edtPwd1.Text, RGrant]);  
  end    
  else  
  begin    
    SQLString:='GRANT %s ON rma.* TO %s@"%%" %s';    
    SQLString:=Format(SQLString, [Right, edtUser.Text, RGrant]);  
  end    
  else    
  ...

Ich hoffe es funzt jetzt...


mettie84 - Do 21.10.04 10:51

Ja das ist mir auch aufgefallen. nur muss man es mit '' statt mit " machen. also ''%%'' dann funzt es.

Thx 2 U