Autor Beitrag
bl3nder
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58



BeitragVerfasst: Mi 09.05.07 09:23 
Hi,

In meinem Programm moechte ich Daten in eine mysql Datenbank einfuegen.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
Form1.SQLQuery.Close;
Form1.SQLQuery.SQL.Text := 'INSERT INTO '+Form1.SQLConnection.Database+'.Person (Name) Values (trim("';
Form1.SQLQuery.SQL.Text := Form1.SQLQuery.SQL.Text + Form3.Ed_Name.Text + '"))';
Form1.SQLQuery.ExecSQL;


Beim Einfuegen ueber das Programm, welches mit Hilfe der Zeos Komponente auf die Datenbank zugreift, entsteht im Eintrag ein Sonderzeichen im Text der Spalte "Name"
Beispiel:

Ich lege im Programm eine neue Person mit dem Namen 'test2' an.
In der Tabelle finde ich danach wie erwartet einen neuen Eintrag.
Allerdings traegt dieser Eintrag in der Spalte "Name" nicht den Namen "test2",
sondern "¶test2".
Das gibt ein Problem beim weiteren Zugriff auf den Datensatz.
An was koennte das liegen ?
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Mi 09.05.07 09:26 
Versuch mal mit Paramtern zu arbeiten, statt die Abfrage zusammenzustückeln, vielleicht bringt dich das weiter (und machts außerdem übersichtlicher)

_________________
Bravery calls my name in the sound of the wind in the night...
bl3nder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58



BeitragVerfasst: Mi 09.05.07 09:32 
du meinst vorher Form3.Ed_Name.Text in eine Variable zu speichern um sie dann dem Query zu uebergeben oder was meinst du mit Parameter ?

Btw: Kopiere ich die Row Values der Spalte und fuege ihn in Word ein , bekomme ich bei der "¶test2" Zeile einen Absatz bevor "test"
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Mi 09.05.07 09:41 
Ich kenn die ZEOS-Komponenten so garnicht, aber mal ein beispiel mit der schlechten alten BDE:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
const
  SQL_INSERT = 'insert into Tabelle (id, name) values (:id,:name)';
begin
  qry.SQL.Text := SQL_INSERT;
  qry.ParamByName('id').asInteger := AId;
  qry.ParamByName('name').asString := AName;
  ...
end;


Sowas in der Art müsste es eigentlich auch bei ZEOS geben.

_________________
Bravery calls my name in the sound of the wind in the night...
bl3nder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58



BeitragVerfasst: Mi 09.05.07 09:52 
Hm Nein das geht bei Zeos nicht..
Aber ich glaube nicht, dass eine andere Schreibweise die Sachen großartig aendern wuerde.. Er schreibt die Daten ja rein.. und im Debugger ist der Text vom Editfeld auch richtig..
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Mi 09.05.07 09:56 
Zeos kann keine Parameter? Wäre für mich ein Grund, sie nicht zu benutzen... naja, egal. Lass dir den kompletten SQL-String mal ausgeben, ob da der Absatz schon drin ist.

_________________
Bravery calls my name in the sound of the wind in the night...
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Mi 09.05.07 10:11 
Zeos kann definitiv Parameter.

_________________
Markus Kinzler.
bl3nder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58



BeitragVerfasst: Mi 09.05.07 10:16 
Müsste trotzdem auch so gehen, habe fast alles in dieser Form erledigen koennen..

Wenn ich nun im Debugger meinen SQLString ausgebe, bevor ich in exec, bekomme ich folgenden String:

'INSERT INTO hwpool.Person (`Name`) Values (trim("'#$D#$A'abc"))'#$D#$A
noidic
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 851

Win 2000 Win XP Vista
D7 Ent, SharpDevelop 2.2
BeitragVerfasst: Mi 09.05.07 10:19 
Jo, #$D#$A is nen Zeilenumbruch, da hast deinen Absatz. Wo der allerdings herkommt, kann ich dir auch nicht sagen. Stehen die Zeichen auch schon im Text-Property vom edit?

_________________
Bravery calls my name in the sound of the wind in the night...
bl3nder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 58



BeitragVerfasst: Mi 09.05.07 10:29 
Nein aber jetzt weiß ich wo der Fehler ist:

Setze ich den String in einer Zeile zusammen, so bekomme ich kein Zeilenumbruch....
Komisch irgendwie:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
//In einer Zeile in Delphi---
Form1.SQLQuery.SQL.Text := 'INSERT INTO '+Form1.SQLConnection.Database+'.Person (`Name`) Values (trim("'+
Form3.Ed_Name.Text + '"))';
//---

Form1.SQLQuery.ExecSQL;



---> 'INSERT INTO hwpool.Person (`Name`) Values (trim("uuuuu"))'#$D#$A



naja jetz gehts...
Dankö
Stefan.Buchholtz
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 612

WIN 2000, WIN XP, Mac OS X
D7 Enterprise, XCode, Eclipse, Ruby On Rails
BeitragVerfasst: Mi 09.05.07 10:39 
Der zeilenumbruch wird durch das SQL-Property der Query eingefügt. Das ist eine Stringlist, und die beendet automatisch jede Zeile mit CR/LF.

ausblenden Delphi-Quelltext
1:
2:
Form1.SQLQuery.SQL.Text := 'INSERT INTO '+Form1.SQLConnection.Database+'.Person (Name) Values (trim("';  // hier wird automatisch der Zeilenumbruch an deinen übergebenen String angehängt
Form1.SQLQuery.SQL.Text := Form1.SQLQuery.SQL.Text + Form3.Ed_Name.Text + '"))';


Ich würde dir im übrigen ebenfalls raten, die Query nicht auf diese Weise zusammenzubauen, sondern Parameter zu verwenden. Insbesondere direkt vom Benutzer eingegebene Daten ungefiltert in einen SQL-String zu übernehmen ist eine schlechte Idee, weil der Benutzer Fehler provozieren und möglicherweise dein SQL-Statement ändern kann, indem er Anführungszeichen eingibt. Unter Umständen ist es damit sogar möglich, beliebige SQL-Statements direkt auszuführen.

Stefan

_________________
Ein Computer ohne Windows ist wie eine Schokoladentorte ohne Senf.