delphi-6

how to use the function "copy" to assign a UTF-8 value to a variable


I use the following code to assign text to an SQL parameter:

quInsert.Parameters.ParamByName('veh_type').value := copy(s,11,1);

Sometimes the text in the .txt file I am reading from is in UTF-8, which has characters using two bytes. How can I change the following code to accept UTF-8 data?

procedure TPrepareform.Button1Click(Sender: TObject);
var
  f : textfile;
  s : string;
  i : integer;
  IsAnsiString : Boolean;
begin
  with quInsert do
  begin
    close;
    sql.clear;
    sql.add('insert into v_info_2018');
    sql.add('(record_type, plate_group, plate_no, v_type, v_type_cn,');
    sql.add('v_type_pt, v_type_pt, v_brand, engine_no, ct_tax_amount');
    sql.add('inspect_date, record_date');
    sql.add('values (:record_type, :plate_group, :plate_no, :v_type, :v_type_cn, :v_type_pt, :v_type_pt,');
    sql.add(':v_brand, :engine_no, :ct_tax_amount,');
    sql.add(':inspect_date, :record_date');
  end;
  i := 0;
  assignfile(f,edMasterName.Text);
  reset(f);
  while not eof(f) do
  begin
    readln(f,s);
    inc(i);
  end;
  prInsert.Max := i;
  i := 0;
  reset(f);
  VRS_Main.StatusBar1.Panels[5].Text:='0';
  while not eof(f) do
  begin
    inc(i);
    prInsert.StepIt;
    readln(f,s);
    quInsert.Parameters.ParamByName('record_type').value := copy(s,1,1);
    quInsert.Parameters.ParamByName('plate_group').value := copy(s,2,1);
    quInsert.Parameters.ParamByName('plate_no').value := copy(s,3,8);
    quInsert.Parameters.ParamByName('v_type').value := copy(s,11,1);
    quInsert.Parameters.ParamByName('v_type_cn').value := UTF8Decode(copy(s,12,50));
    quInsert.Parameters.ParamByName('v_type_pt').value := copy(s,62,50);
    quInsert.Parameters.ParamByName('v_brand').value := copy(s,112,30);
    quInsert.Parameters.ParamByName('engine_no').value := copy(s,142,50);
    quInsert.Parameters.ParamByName('ct_tax_amount').value := copy(s,192,9);
    quInsert.Parameters.ParamByName('inspect_date').value := copy(s,201,50);
    quInsert.Parameters.ParamByName('record_date').value := copy(s,251,8);
    quInsert.ExecSQL;
    VRS_Main.StatusBar1.Panels[5].Text:=inttostr(strtoint(VRS_Main.StatusBar1.Panels[5].Text)+1);
  end;
end;

image


Solution

  • Store the UTF-8 data in a UTF8String instead of a standard String, and then call either UTF8Decode() or Utf8ToAnsi() and store that result in the database.