【问题标题】:how to use the function "copy" to assign a UTF-8 value to a variable如何使用“复制”函数将 UTF-8 值分配给变量
【发布时间】:2017-08-24 02:29:43
【问题描述】:

我使用以下代码将文本分配给 SQL 参数:

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

有时,我正在读取的 .txt 文件中的文本是 UTF-8 格式,其中包含使用两个字节的字符。如何更改以下代码以接受 UTF-8 数据?

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;

【问题讨论】:

  • 数据库使用什么编码?它甚至被配置为接受 Unicode 吗?
  • 是windows sql server 2008 R2
  • 您的 SQL 格式错误。在这两行:sql.add('inspect_date, record_date');sql.add(':inspect_date, :record_date'); 上,您分别在 record_date:record_date 之后缺少一个结束 )。您的 SQL 在执行时看起来像这样:'insert into v_info_2018 (<fields> values (<params>',当它应该看起来像这样时:'insert into v_info_2018 (<fields>) values (<params>)' 我很惊讶您没有从数据库中收到关于此的运行时错误。

标签: delphi-6


【解决方案1】:

将 UTF-8 数据存储在 UTF8String 而不是标准的 String 中,然后调用 UTF8Decode()Utf8ToAnsi() 并将结果存储在数据库中。

【讨论】:

  • 嗨,亲爱的雷米,谢谢你的回答,这很有用。临时字符串:=复制(s,12,50); tempstring := UTF8Decode(tempstring); quInsert.Parameters.ParamByName('veh_type_cn').value := tempstring;第二行字符串显示异常的“中文文本”,当在第三行时,quInsert.Parameters.ParamByName('veh_type_cn').value 仍然得到奇怪的文本,你知道为什么吗?
  • UTF8Decode() 返回 UTF-16 WideString。不要将其分配回您的tempString(显然是(Ansi/UTF8)String),这只会执行不需要的UTF16-> Ansi 转换。将WideString 直接分配给Value 并让数据库处理它需要的任何转换,例如:quInsert.Parameters.ParamByName('v_type_cn').value := UTF8Decode(copy(s,12,50));(顺便说一句,在您的问题中,您调用的是UTF8Encode() 而不是UTF8Decode()。不要重复-编码 UTF 数据!)
  • i.stack.imgur.com/oP3l3.png 嗨 Remy,你能帮忙看看这张照片吗,看起来价值仍然是“?”
  • 您正在调试器中评估ValueVariant)。也许 Delphi 6 中的工具提示评估器不能很好地处理 Unicode 字符串?谁知道。您还没有显示您正在使用的实际数据,或者它在每个步骤中的样子。您是否确认WideString 本身包含正确的数据?插入后的数据库呢?你没有回答大卫的问题。你用的是什么数据库?它使用什么字符编码?这些是重要的事情要知道。您的数据要经过多层,其中任何一层都可能发生数据丢失。
  • 是windows sql server 2008 R2
猜你喜欢
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多