Richedit Delphi messes up text when writing to Postgresql database

Asked

Viewed 533 times

1

I’ve tried almost everything.

I have a system in Delphixe6 with Postgresql, in it I have some Text type fields that I need to record formatted text (bold, italics, background color, etc) like word, it works blz, except the following, when I write the text for the first time it writes correctly, but if I edit the text it records a lot of ??????????????????? interrogations messing up everything. The way I load text in the editor is below:

  if (zqrOrcItens.RecordCount = 0) then
  begin
    TFuncoes.pMensagem('Não existem itens na lista para editar');
    Exit;
  end;

  fdm.Salvar := False;
  fdm.RichStream := TStringStream.Create('');
  zqrOrcItensTexto.SaveToStream(fdm.RichStream);

  f_richedit := Tf_richedit.Create(nil);
  fdm.RichStream.Position := 0;
  f_richedit.RichEdit.Lines.LoadFromStream(fdm.RichStream);
  TFuncoes.pJustRichEdit(f_richedit.RichEdit, False);
  f_richedit.ShowModal;

  if (fdm.Salvar) then
  begin
    zqrOrcItens.Edit;
    fdm.RichStream.Position := 0;
    zqrOrcItensTexto.LoadFromStream(fdm.RichStream); // AsString := fdm.RichStream.DataString;
    zqrOrcItenstexto_puro.AsAnsiString := fdm.TextoPuro;
    zqrOrcItens.Post;
    fdm.zConecta.Commit;
  end;
  FreeAndNil(fdm.RichStream);

I save the formatted text and the plain text so as not to lose what was typed, the plain text records in a good, but the formatted mess with ?????????????

Damn time I went to choose Richedit to save texts, but this system is many years old and at the time was what I had option, the base is already loaded with these texts and can not edit everything and turn into html

I need a tip on how to work with Richedit and not have that problem

I have tested many types of Charsets today use UTF8, which should solve the problem but not solved.

1 answer

1

The problem of recording like this is the type of your field in the database. Instead of using the TEXT type use the BYTEA type. Until today I never had problems with this type and Richedit, including use with images and complete formatting.

To save I use only this:

procedure TForm1.Button1Click(Sender: TObject);
var
  s: TMemoryStream;
begin
  s := TMemoryStream.Create;
  RichEdit1.Lines.SaveToStream(s);

  s.Seek(0, soFromBeginning);

  FDQuery1.Edit;
  FDQuery1CampoByteA.LoadFromStream(s);
  FDQuery1.Post;
end;
  • Yeah... I’m screwed I used Text field to store the source code of Richedit thinking it was HTML, but Richedit brings a lot of junk together in memory and these sticks... I’ll have to think of a strategy, are very important documents already recorded

  • 1

    Gives a searched for Text2bytea that will find some methods to perform field conversion or open another topic here in SOPT.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.