AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Datenbanken MySQL Query Update funktioniert nicht
Thema durchsuchen
Ansicht
Themen-Optionen

MySQL Query Update funktioniert nicht

Ein Thema von akio87 · begonnen am 28. Okt 2015 · letzter Beitrag vom 2. Nov 2015
Antwort Antwort
akio87

Registriert seit: 18. Mär 2010
44 Beiträge
 
Delphi XE8 Architect
 
#1

AW: MySQL Query Update funktioniert nicht

  Alt 28. Okt 2015, 18:08
Ändert sich etwas, wenn ich in der Datenbank die Spalten "Standard" -> "Kein(e)" anpasse und bei "NULL" den haken entferne?

Hab es zwar gerade getestet, scheint so ohne weiteres nicht zu funktionieren...

Spalten auf Null abfragen? Dies bräuchte ich eigentlich dann nur für "Adress1, Adress2, Such1, Such2"...

Wie baut man sowas jetzt da mit ein? So in etwa:

Code:
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Adress1 = :Adress1 or spalte is Null');
  Mit Zitat antworten Zitat
nahpets
(Gast)

n/a Beiträge
 
#2

AW: MySQL Query Update funktioniert nicht

  Alt 28. Okt 2015, 18:11
jo, genau so, wie in Deinem Beispiel.
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Adress1 = :Adress1 or Adress1 is Null');
  Mit Zitat antworten Zitat
akio87

Registriert seit: 18. Mär 2010
44 Beiträge
 
Delphi XE8 Architect
 
#3

AW: MySQL Query Update funktioniert nicht

  Alt 28. Okt 2015, 18:21
Klappt nur zu hälfte...

jetzt erscheinen "0" in den leeren Zellen...

Jetzt vielleicht etwas in der Datenbank anpassen?

EDIT:
Updatefunktion geht wieder an den stellen nicht. Es bleibt jetzt "0":

Zitat:

MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('update `' + Tabelle + '`');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('set');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Firma = :Firma,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 = :Adress1 or Adress1 is Null,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress2 = :Adress2 or Adress2 is Null,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Straße = :Straße,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('PLZ = LZ,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Ort = :Ort,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Land = :Land,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such1 = :Such1 or Such1 is Null,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such2 = :Such2 or Such2 is Null');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('where');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Firma = :OLD_Firma and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 = :OLD_Adress1 and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress2 = :OLD_Adress2 and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Straße = :OLD_Straße and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('PLZ = :OLD_PLZ and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Ort = :OLD_Ort and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Land = :OLD_Land and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such1 = :OLD_Such1 and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such2 = :OLD_Such2');

Geändert von akio87 (28. Okt 2015 um 18:25 Uhr)
  Mit Zitat antworten Zitat
nahpets
(Gast)

n/a Beiträge
 
#4

AW: MySQL Query Update funktioniert nicht

  Alt 28. Okt 2015, 18:35
Sorry, da hab' ich wohl was falsch verstanden oder mich ungeschickt ausgedrückt.
Code:
or spalte is null
gehört nur in die Where-Bedingung aber nicht in den Teil, in dem per Set eine Wertzuweisung erfolgt.

In etwa so:
Delphi-Quellcode:
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('update `' + Tabelle + '`');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('set');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Firma = :Firma,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 = :Adress1,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress2 = :Adress2,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Straße = :Straße,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('PLZ = LZ,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Ort = :Ort,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Land = :Land,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such1 = :Such1,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such2 = :Such2');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('where');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Firma = :OLD_Firma and');
if :OLD_Adress1 = 'then begin <-- hier natürlich die Variabel abfragen, die dem Parameter :OLD_Adress1 zugewiesen wird.
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 is null and');
end else begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 = :OLD_Adress1 and');
end;
if :OLD_Adress2 = 'then begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress2 is null and');
end else begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress2 = :OLD_Adress2 and');
end;
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Straße = :OLD_Straße and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('PLZ = :OLD_PLZ and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Ort = :OLD_Ort and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Land = :OLD_Land and');
if :OLD_Such1 = 'then begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such1 = :OLD_Such1 and');
end else begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such1 = :OLD_Such1 and');
end;
if :OLD_Such2 = 'then begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such2 = :OLD_Such2'); Such2 = :OLD_Such2';
end else begin
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add(
'Such2 = :OLD_Such2'); Such2 = :OLD_Such2';
end;
Die IF-Abfragen dürfen natürlich nicht auf die Parameter gehen, sondern auf die Variabeln..., deren Werte den Parametern zugewiesen werden.
  Mit Zitat antworten Zitat
akio87

Registriert seit: 18. Mär 2010
44 Beiträge
 
Delphi XE8 Architect
 
#5

AW: MySQL Query Update funktioniert nicht

  Alt 28. Okt 2015, 19:01
Ah herrje, darauf wäre ich ja nie gekommen

Die Eingabe erfolgt ja direkt in der UniDBGrid, screenshot im Anhang.
Das heißt, ich muss für die Variable in der IF Anfrage dierekt die Zelle ansprechen?

Code:
if UniDBGrid1.??????? = '' then begin //<-- hier natürlich die Variabel abfragen, die dem Parameter :OLD_Adress1 zugewiesen wird.
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 is null and');
UniDBGrid hat leider keine SelectedField funktion
Code:
DBGrid1.SelectedField.AsString;
Und damit scheint es auch nicht zu funktionieren, bleibt wie gehabt "0" und andere werden dann mit Sntaxfehler angezeigt:
Code:
Query1.FieldByName(Feldname).Asxxx

EDIT: 20:48 Uhr
Hab es nun soweit, dass ich Updaten kann, aber er übernimmt noch immer nicht die Null-Strings:
Code:
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Clear;
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('update `' + Tabelle + '`');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('set');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Firma = :Firma,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 = :Adress1,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress2 = :Adress2,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Straße = :Straße,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('PLZ = :PLZ,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Ort = :Ort,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Land = :Land,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such1 = :Such1,');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Such2 = :Such2');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('where');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Firma = :OLD_Firma and');

if MainModule.UniMainModule.mySQLQuery3.FieldByName('Adress1').AsString = '' then begin //<-- hier natürlich die Variabel abfragen, die dem Parameter :OLD_Adress1 zugewiesen wird.
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Adress1 is null and');
end else begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Adress1 = :OLD_Adress1 and');
end;

if MainModule.UniMainModule.mySQLQuery3.FieldByName('Adress2').AsString = '' then begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Adress2 is null and');
end else begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Adress2 = :OLD_Adress2 and');
end;

MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Straße = :OLD_Straße and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('PLZ = :OLD_PLZ and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Ort = :OLD_Ort and');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Land = :OLD_Land and');

if MainModule.UniMainModule.mySQLQuery3.FieldByName('Such1').AsString = '' then begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Such1 is null and');
end else begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Such1 = :OLD_Such1 and');
end;

if MainModule.UniMainModule.mySQLQuery3.FieldByName('Such2').AsString = '' then begin
  MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Such2 is null'); //Such2 = :OLD_Such2';
end else begin
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Add('Such2 = :OLD_Such2'); //Such2 = :OLD_Such2';
end;
Angehängte Grafiken
Dateityp: png Unbenannt.png (20,2 KB, 15x aufgerufen)

Geändert von akio87 (28. Okt 2015 um 19:49 Uhr)
  Mit Zitat antworten Zitat
akio87

Registriert seit: 18. Mär 2010
44 Beiträge
 
Delphi XE8 Architect
 
#6

AW: MySQL Query Update funktioniert nicht

  Alt 28. Okt 2015, 20:17
Mir fällt gerade auf, sollte es nicht ehr sorum lauten, wenn ich den neuen Wert als NULL definieren will, wenn der String leer ist?

Code:
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL.Clear;
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('update `' + Tabelle + '`');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('set');
MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Firma = :Firma,');

if :Adress1 = '' then
 begin
   MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 = NULL,');
 end
else
 begin
   MainModule.UniMainModule.mySQLUpdateSQL1.ModifySQL .Add('Adress1 = :Adress1,');
 end;
Aber wie komme ich an den neuen Wert :Adress1 nach der Eingabe in der DBGrid heran?
  Mit Zitat antworten Zitat
Perlsau
(Gast)

n/a Beiträge
 
#7

AW: MySQL Query Update funktioniert nicht

  Alt 28. Okt 2015, 20:26
Ich weiß nicht, ob das auch in MySQL geht, aber in Firebird und MsSQL mache ich das mit den leeren String-Feldern so, daß ich bereits beim Tabellenentwurf das Feld als NotNull deklariere und dann als Defaultwert '' angebe. Damit wird dann beim insert automatisch '' eingetragen, wenn das entsprechende Feld leer (null) ist. Sollte MySQL keinen Defaultwert unterstützen, kannst du den auch in der Query-Komponente angeben.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:47 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz