Delphi 将数据从 StringGrid 插入数据库 Table

Delphi Insert data from StringGrid to a Database Table

我正在尝试将数据从 StringGrid 插入到 Oracle DB table 为此,我尝试如下所示。

function TfrmMapping.LoadtoTable: Boolean;
var
  I, J: Integer;
  lQuery, s: string;
  lData: TArray<string>;
begin

  for I := 0 to vTableColumns.count - 1 do
  begin
    if I <> vTableColumns.count - 1 then
    begin
      s := s + vTableColumns[I] + ',';
    end
    else
    begin
      s := s + vTableColumns[I];
    end;
  end;

  for I := 1 to StrGrdLoadCSVData.RowCount - 1 do
  begin
    vSortedGrid.Add(StrGrdLoadCSVData.Rows[I].CommaText);
  end;

  for I := 0 to vSortedGrid.count - 1 do
  begin
    lQuery := 'Insert into ' + cmbBXDBTables.Text + '(' + s + ') values(' +
      vSortedGrid[I] + ')';
    DataModSample.FDQuery1.SQL.Clear;
    DataModSample.FDQuery1.SQL.Add(lQuery);
    DataModSample.FDQuery1.ExecSQL;
  end;
  Result := True;
end;

在代码中,我将 StringGrid(StrGrdLoadCSVData) 的所有数据添加到 StringList(vSortedGrid),现在我试图遍历 StringList 以将每一行添加到数据库,但我无法插入,因为我正在采用这样的值

Insert into abc(sno,Name)values(1,welcome);

因为 welcome 没有引号所以出错了。

错误是这样的:[FireDAC][Phys][Ora]ORA-00984:column not allowed here

如何修改我的代码以将数据成功插入 Db。

编辑

我的 table 结构是:

Name            Type
 ---------  ------------
 SNO          NUMBER(38)
 NAME         VARCHAR2(15)

我在 table 中想要的结果应该是这样的:

       SNO NAME
---------- ----------
         1 Hello
         2 Welcome

table 中的值来自字符串 List

It is because there are no quotes to welcome it is giving an error.

所以根据你所说的 :

  for I := 0 to vSortedGrid.count - 1 do
  begin
    lQuery := 'Insert into ' + cmbBXDBTables.Text + '(' + s + ') values('+IntToStr(i+1)+',' +
      QuotedStr(vSortedGrid[I]) + ')';
    DataModSample.FDQuery1.SQL.Clear;
    DataModSample.FDQuery1.SQL.Add(lQuery);
    DataModSample.FDQuery1.ExecSQL;
  end;
  Result := True;
end;

注:最好用parameters

更新:

使用 TFDTableTStringGrid 插入的另一个选项:

procedure TForm1.Button2Click(Sender: TObject);
Var I : Integer;
begin

for i := 1 to StringGrid1.RowCount-1 do

    begin
     try
      FDTable1.Append;
      FDTable1SNO.Value := StrToInt( StringGrid1.Cells[0,i] );
      FDTable1SName.Value := StringGrid1.Cells[1,i];
      FDTable1.Post;
      except on E: Exception do
        begin
         MessageDlg(E.Message,mtError,[mbOK],0);
         MessageBeep(MB_ICONERROR);
       end;
    end;
end;

使用 TFDQueryTStringGrid 插入的另一个选项(避免 SQL 注入):

procedure TForm1.Button1Click(Sender: TObject);
Var I : Integer;   TableName : String;
begin

TableName := 'Table1';

for i := 1 to StringGrid1.RowCount-1 do

    begin
     try
      FDQuery1.SQL.Text := 'Insert Into '+TableName+' Values(:Val1 , :Val2)' ;
      FDQuery1.Params.ParamByName('Val1').Value := StrToInt( StringGrid1.Cells[0,i] );
      FDQuery1.Params.ParamByName('Val2').Value := StringGrid1.Cells[1,i];
      FDQuery1.ExecSQL;
     except on E: Exception do
      begin
       MessageDlg(E.Message,mtError,[mbOK],0);
       MessageBeep(MB_ICONERROR);
      end;
    end;

您也可以根据需要 Create parameters Runtime 例如:

FDQuery1.Params.CreateParam(ftString,'ParamName',ptInput) ;

您还可以使用 GetTableNames() 获取 Database 中的所有表。

我修改了如下代码

function TfrmMapping.LoadtoTable: Boolean;
var
  I, J: Integer;
  lQuery, s, lcolvalues: string;
begin

  for I := 0 to vTableColumns.count - 1 do
  begin
    if I <> vTableColumns.count - 1 then
    begin
      s := s + vTableColumns[I] + ',';
    end
    else
    begin
      s := s + vTableColumns[I];
    end;
  end;

  for I := 1 to StrGrdLoadCSVData.RowCount - 1 do
  begin
    for J := 0 to vTableColumns.count - 1 do
    begin
      if J <> vTableColumns.count - 1 then
      begin
        lcolvalues := lcolvalues +
          QuotedStr(StrGrdLoadCSVData.Cells[J, I]) + ',';
      end
      else
      begin
        lcolvalues := lcolvalues + QuotedStr(StrGrdLoadCSVData.Cells[J, I]);
      end;
    end;
    lQuery := 'Insert into ' + cmbBXDBTables.Text + '(' + s + ') values (' +
      lcolvalues + ')';
    DataModSample.FDQuery1.SQL.Clear;
    DataModSample.FDQuery1.SQL.Add(lQuery);
    DataModSample.FDQuery1.ExecSQL;

    lcolvalues := '';
  end;
  Result := True;
end;

这是从字符串网格向 Table 插入值,到目前为止我还没有使用参数传递。我也必须尝试确保更多安全性。

谢谢@Sami,使用您的 FDQuery 概念购买我有这个想法...