Delphi SQLite错误:约束失败

时间:2017-11-05 16:07:45

标签: sqlite delphi zeos

我遇到了这个常见错误,无法确定导致该错误的原因

这是从CSV文件

导入数据的过程
procedure TForm1.importarData;
var
  S,D: TStringList;
  fecha:TDate;
  hora:TTime;
  i,j: Integer;
  FileName,Aux: String;
  fExport : TextFile;
  query:String;
  rcDb:TextFile;
begin
  S := TStringList.Create;
  D := TStringList.Create;

  D.Clear;
  S.Clear;

  OpenDialog1.Filter := 'Only CSV files (*.csv)|*.csv';
  FileName:='';
  OpenDialog1.FileName:='';

  //fecha:=nil;
  if OpenDialog1.Execute then
    begin
      S.LoadFromFile(OpenDialog1.FileName);

      for I := 1 downto S.Count-1 do
        begin
          D.Clear;
          D.CommaText := s[i];

          for J := 0 to D.Count-1 do
            D[J] := StringReplace(D[J], '_', ' ', [rfReplaceAll, rfIgnoreCase]);


          FormatSettings.ShortDateFormat:= 'dd mm yyyy';

            //260234920,John_Smith,Jane_Smith,Janet_Smith,Ryan_Smith,201_CENTRE_STREET,Nowhere,AB,T0J111,403-555-5555,smith@email.com,200,2523,2017,12345678,12-10-1971,12-10-1987,12-10-2003,12-10-2004

          //Showmessage(D[1]);

          query:='Insert Into RFID(CID,PrM,AddM1,AddM2,AddM3,Address,City,Province,Postal,Phone,Email,Paid,MID,MYear,MDob,MDob1,MDob2,MDob3,Pal) Values(';                //    "'++'",
          query:=query+'"'+D[0]+'",';
          query:=query+'"'+D[1]+'",';
          query:=query+'"'+D[2]+'",';
          query:=query+'"'+D[3]+'",';
          query:=query+'"'+D[4]+'",';
          query:=query+'"'+D[5]+'",';
          query:=query+'"'+D[6]+'",';
          query:=query+'"'+D[7]+'",';
          query:=query+'"'+D[8]+'",';
          query:=query+'"'+D[9]+'",';
          query:=query+'"'+D[10]+'",';
          query:=query+'"'+D[11]+'",';
          query:=query+'"'+D[12]+'",';
          query:=query+'"'+D[13]+'",';
          query:=query+'"'+formatdatetime('yyyy-mm-dd', StrToDate(D[15]))+'",';
          query:=query+'"'+formatdatetime('yyyy-mm-dd', StrToDate(D[16]))+'",';
          query:=query+'"'+formatdatetime('yyyy-mm-dd', StrToDate(D[17]))+'",';
          query:=query+'"'+formatdatetime('yyyy-mm-dd', StrToDate(D[18]))+'",';
          query:=query+'"'+D[14]+'"';
          query:=query+')';

          ZQuery1.Close;
          ZQuery1.SQL.Clear;
          ZQuery1.SQL.Add(query);
          ZQuery1.ExecSQL;
          //showmessage('Done')
        end;
    end;
  S.Free;
  D.Free;
end;

执行SQL

之前的查询结果
  

'插入   RFID(CID,PRM,ADDM1,AddM2,AddM3,地址,城市,省,邮政,电话,电子邮件,支付,MID,MYear,MDob,MDob1,MDob2,MDob3,PAL)   价值观(“260234920”,“约翰史密斯”,“简史密斯”,“珍妮特史密斯”,“瑞恩   史密斯“,”201 CENTER   STREET”, “无处”, “AB”, “T0J111”, “403-555-5555”, “smith@email.com”, “200”, “2523”, “2017年”, “1971年10月12日” “1987年10月12日”, “2003-10-12”, “2004-10-12”, “12345678”)'

这是来自SQLite数据库的DDL

CREATE TABLE [RFID](
    [CID] VARCHAR(16), 
    [PrM] VARCHAR(32), 
    [AddM1] VARCHAR(32), 
    [AddM2] VARCHAR(32), 
    [AddM3] VARCHAR(32), 
    [Address] VARCHAR(512), 
    [City] VARCHAR(64), 
    [Province] VARCHAR(64), 
    [Postal] VARCHAR(16), 
    [Phone] VARCHAR(32), 
    [Email] VARCHAR(256), 
    [Paid] VARCHAR(8), 
    [MID] VARCHAR(4), 
    [MYear] VARCHAR(4), 
    [MDob] DATE, 
    [MDob1] DATE, 
    [Mdob2] DATE, 
    [MDob3] DATE, 
    [PAL] VARCHAR(64));

1 个答案:

答案 0 :(得分:1)

我通过创建一个名为ID类型为intger的新字段作为主键来修复此问题,而不是null并且使用自动增量,如下所示:

CREATE TABLE [RFID](
    [ID] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
    [CID] INTEGER(16) UNIQUE, 
    [PrM] VARCHAR(32), 
    [AddM1] VARCHAR(32), 
    [AddM2] VARCHAR(32), 
    [AddM3] VARCHAR(32), 
    [Address] VARCHAR(512), 
    [City] VARCHAR(64), 
    [Province] VARCHAR(64), 
    [Postal] VARCHAR(16), 
    [Phone] VARCHAR(32), 
    [Email] VARCHAR(256), 
    [Paid] VARCHAR(8), 
    [MID] VARCHAR(4), 
    [MYear] VARCHAR(4), 
    [MDob] DATE, 
    [MDob1] DATE, 
    [Mdob2] DATE, 
    [MDob3] DATE, 
    [PAL] VARCHAR(64));