Delphi Insert data from StringGrid into database table

I am trying to insert data from StringGrid into an Oracle DB table, for which I tried, as shown below.

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;

In the code, I add all the data of StringGrid (StrGrdLoadCSVData) to StringList (vSortedGrid), and now I try to skip StringList to add each row to the database, but I couldn’t insert because mine is taking values ​​like this

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

This is because there are no quotes in it welcome, it gives an error.

this is mistake: [FireDAC][Phys][Ora]ORA-00984:column not allowed here

How can I change my code to successfully insert data into Db.

EDIT

My table structure:

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

my desired result in the table should look like this:

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

Values ​​in the table starting with List

+4
source share
2

,

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;

, . .

@Sami, FDQuery. ...

+2

, , , .

, , :

  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.

Update:

TStringGrid TFDTable:

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;

TStringGrid TFDQuery ( SQL Injection):

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.

+2

Source: https://habr.com/ru/post/1667245/


All Articles