dcrudo
asked on
Delphi Tmemo with more than one line into Mysql
Dear Experts,
I have some fields on a form, let's say one Tedit and Tmemo fileds.
Edit1 has as text "Customer 1"
Memo1 has as text
"south street 1
Las vegas"
Now I want to insert them into MySQL and I use the following statement:
query.SQL.text := 'insert into test (name, address) VALUES ("'+edit1.text + '","' + Memo1.Text + "'";
But I always get an error that the query was not successfull... getting the SQL string it shows:
'insert into test (name, address) VALUES ("Customer1",""south street1
Las Vegas"");
It is like when the memo contains more than one line then getting the memo text will add quotes at the
beginning and at the end...
Any idea on how to solve this issue?
Thank you.
Dave
I have some fields on a form, let's say one Tedit and Tmemo fileds.
Edit1 has as text "Customer 1"
Memo1 has as text
"south street 1
Las vegas"
Now I want to insert them into MySQL and I use the following statement:
query.SQL.text := 'insert into test (name, address) VALUES ("'+edit1.text + '","' + Memo1.Text + "'";
But I always get an error that the query was not successfull... getting the SQL string it shows:
'insert into test (name, address) VALUES ("Customer1",""south street1
Las Vegas"");
It is like when the memo contains more than one line then getting the memo text will add quotes at the
beginning and at the end...
Any idea on how to solve this issue?
Thank you.
Dave
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
What are the field dtypes for name and address?
ASKER
in the database the fields types are:
name : varchar 255
address : text
to give you a better idea let me explain more:
The following is the resulting query:
insert into ana_customers (id,firstname, lastname, civils, address, zip, city, tel_home,tel_mobile, tel_fax, email, no_avs, doctor_name, insurance_name, insurance_number, treatment_def, diagnosis, agreed_hours, def_positions, active,birth, treatment_start, treatment_end) VALUES ("109","Dave","Ohara","Mar ried",""La s vegas"","","","","","","", "","",""," ","","","9 0","","-1" ,"1980-05- 25","0000- 00-00","00 00-00-00")
as you see near 'Las Vegas' quotes are doubled... this is the only field of type MEMO... to insert those fields i use the following function:
Function TDM1.dbins(tablex,variable x : String; valuex: Variant): string;
var sql : string;
Begin
valuex := ReplaceStr(valuex,'''',''' '''); // Double quotes in case one field contains them
valuex := ReplaceStr(valuex,char(186 ),'","'); //use ASCII code 186 to delimit fields
sql := 'insert into ' + tablex + ' ('+ variablex + ') VALUES ("' + valuex + '")';
qry_dbins.SQL.Text := sql;
Data.DM1.InfoBox(sql,1,1);
try
begin
qry_dbins.Execute;
result := '0';
end;
Except On E:Exception Do
begin
Data.DM1.InfoBox('SQL Error: '+E.message,1,0);
Data.DM1.InfoBox('-------- ---------- ---------- ---------- ---------- ---------- --------', 1,0);
Data.DM1.InfoBox(sql,1,1);
result := '1';
end;
end;
end;
To collect all information from the form, I put all fields in a TstringList:
procedure TForm1.suiButton3Click(Sen der: TObject);
var strlVal : TstringList;
x : Integer;
Fields : String;
begin
// Start Customer Insert
strlVal := TStringList.Create;
strlVal.Clear;
strlVal.Delimiter := char(186);
strlVal.Add(Data.DM1.nextI d('cus'));
strlVal.Add(ed_c_firstname .Text);
strlVal.Add(ed_c_lastname. Text);
strlVal.Add(ed_c_civils.Te xt);
strlVal.Add(ed_c_address.E ditingValu e);
strlVal.Add(ed_c_zip.Text) ;
strlVal.Add(ed_c_city.Text );
strlVal.Add(ed_c_tel_home. Text);
strlVal.Add(ed_c_tel_mobil e.Text);
strlVal.Add(ed_c_tel_fax.T ext);
strlVal.Add(ed_c_email.Tex t);
strlVal.Add(ed_c_no_avs.Te xt);
strlVal.Add(ed_c_doctor_na me.Text);
strlVal.Add(ed_c_insurance _name.Text );
strlVal.Add(ed_c_insurance _number.Te xt);
strlVal.Add(ed_c_treatment _def.Text) ;
strlVal.Add(ed_c_diagnosis .Text);
strlVal.Add(ed_c_agreed_ho urs.Text);
strlVal.Add(ed_c_def_posit ions.Text) ;
strlVal.Add(BoolToStr(ed_c _active.Ch ecked));
strlVal.add(FormatDateTime ('yyyy/mm/ dd',ed_c_b irth.Curre ntDate));
strlVal.Add(FormatDateTime ('yyyy/mm/ dd',ed_c_t reatment_s tart.Curre ntDate));
strlVal.Add(FormatDateTime ('yyyy/mm/ dd',ed_c_t reatment_e nd.Current Date));
Fields := 'id,firstname, lastname, civils, address, zip, city, tel_home,'+
'tel_mobile, tel_fax, email, no_avs, doctor_name, insurance_name, '+
'insurance_number, treatment_def, diagnosis, agreed_hours, def_positions, active,'+
'birth, treatment_start, treatment_end';
try
Begin
Data.DM1.dbins('ana_custom ers',Field s, strlVal.DelimitedText);
end;
except
Begin
end;
end;
end;
name : varchar 255
address : text
to give you a better idea let me explain more:
The following is the resulting query:
insert into ana_customers (id,firstname, lastname, civils, address, zip, city, tel_home,tel_mobile, tel_fax, email, no_avs, doctor_name, insurance_name, insurance_number, treatment_def, diagnosis, agreed_hours, def_positions, active,birth, treatment_start, treatment_end) VALUES ("109","Dave","Ohara","Mar
as you see near 'Las Vegas' quotes are doubled... this is the only field of type MEMO... to insert those fields i use the following function:
Function TDM1.dbins(tablex,variable
var sql : string;
Begin
valuex := ReplaceStr(valuex,'''','''
valuex := ReplaceStr(valuex,char(186
sql := 'insert into ' + tablex + ' ('+ variablex + ') VALUES ("' + valuex + '")';
qry_dbins.SQL.Text := sql;
Data.DM1.InfoBox(sql,1,1);
try
begin
qry_dbins.Execute;
result := '0';
end;
Except On E:Exception Do
begin
Data.DM1.InfoBox('SQL Error: '+E.message,1,0);
Data.DM1.InfoBox('--------
Data.DM1.InfoBox(sql,1,1);
result := '1';
end;
end;
end;
To collect all information from the form, I put all fields in a TstringList:
procedure TForm1.suiButton3Click(Sen
var strlVal : TstringList;
x : Integer;
Fields : String;
begin
// Start Customer Insert
strlVal := TStringList.Create;
strlVal.Clear;
strlVal.Delimiter := char(186);
strlVal.Add(Data.DM1.nextI
strlVal.Add(ed_c_firstname
strlVal.Add(ed_c_lastname.
strlVal.Add(ed_c_civils.Te
strlVal.Add(ed_c_address.E
strlVal.Add(ed_c_zip.Text)
strlVal.Add(ed_c_city.Text
strlVal.Add(ed_c_tel_home.
strlVal.Add(ed_c_tel_mobil
strlVal.Add(ed_c_tel_fax.T
strlVal.Add(ed_c_email.Tex
strlVal.Add(ed_c_no_avs.Te
strlVal.Add(ed_c_doctor_na
strlVal.Add(ed_c_insurance
strlVal.Add(ed_c_insurance
strlVal.Add(ed_c_treatment
strlVal.Add(ed_c_diagnosis
strlVal.Add(ed_c_agreed_ho
strlVal.Add(ed_c_def_posit
strlVal.Add(BoolToStr(ed_c
strlVal.add(FormatDateTime
strlVal.Add(FormatDateTime
strlVal.Add(FormatDateTime
Fields := 'id,firstname, lastname, civils, address, zip, city, tel_home,'+
'tel_mobile, tel_fax, email, no_avs, doctor_name, insurance_name, '+
'insurance_number, treatment_def, diagnosis, agreed_hours, def_positions, active,'+
'birth, treatment_start, treatment_end';
try
Begin
Data.DM1.dbins('ana_custom
end;
except
Begin
end;
end;
end;
Is ed_c_address a TMemo? I'm not familiar with the "EditingValue". Should this not be
strlVal.Add(ed_c_address.T ext);
strlVal.Add(ed_c_address.T
ASKER
With:
strlVal.Add(ed_c_address.T ext);
I get the same result... the quotes are doubled... I wonder if this could be an effect of the TStringList where I store the variables... that when it finds a 'new line' than it will add the quotes to everything...
any idea?
thank you!
strlVal.Add(ed_c_address.T
I get the same result... the quotes are doubled... I wonder if this could be an effect of the TStringList where I store the variables... that when it finds a 'new line' than it will add the quotes to everything...
any idea?
thank you!
Looks fine to me. Only thing I can think of is that the string you add to the Stringlist contains the "
After this call: strlVal.Add(ed_c_address.E ditingValu e);
Check if StrlVal[4]
contains this: "Las vegas"
or this: Las vegas
After this call: strlVal.Add(ed_c_address.E
Check if StrlVal[4]
contains this: "Las vegas"
or this: Las vegas
Can you show me your ReplaceStr function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Problem solved!
This is what happens:
when a TStringlist contains a new line like the following:
Street 1
las vegas
quotes are automatically added to the string (it does not happens if the line is only one!)
To solve it:
I've found this property on the TstringList object: StringList1.QuoteChar
StringList1.Quotechar := char(187);
and then when I have the complete insert statement ready, before executing the query I do:
ReplaceStr(valuex,char(187 ),'');
and will do the trick...
char 187 is a symbol that usually is not used when writing an addres ;)
Thank you very much!!
Dave.
This is what happens:
when a TStringlist contains a new line like the following:
Street 1
las vegas
quotes are automatically added to the string (it does not happens if the line is only one!)
To solve it:
I've found this property on the TstringList object: StringList1.QuoteChar
StringList1.Quotechar := char(187);
and then when I have the complete insert statement ready, before executing the query I do:
ReplaceStr(valuex,char(187
and will do the trick...
char 187 is a symbol that usually is not used when writing an addres ;)
Thank you very much!!
Dave.
ASKER
Jacco,
thx a lot for your time too!!
I will assign to both of you some points... thank you very much!!
Dave.
thx a lot for your time too!!
I will assign to both of you some points... thank you very much!!
Dave.
ASKER
sorry... it was a mistake in writing the string ... I didn't copy paste...just copy...because my query is a little more complex ;)
yes, my string is delimited as you wrote... and it gives me the error above...
any idea?
Thank you!!