Question

I'm with a problem on Delphi, i create a simple app to test the mobile power of RadStudio, I created a simple app that put some data into some inputs and then add it to database when button is clicked. I followed this Embarcadero tutorial as starting point

The problem is that I only get one entry added, then no more entries are added or the list is not refreshed. Below some code:

Table creation:

procedure TTabbedForm.logAfterConnect(Sender: TObject);
 begin
  log.ExecuteDirect('CREATE TABLE IF NOT EXISTS lista (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,idCons INTEGER,nome TEXT,kms INTEGER,kmsAlarme INTEGER,quantidade INTEGER,quantidadeAlarme INTEGER,data INTEGER,dataAlarme INTEGER,alarmeMsg TEXT)');
 end;

Add button code:

procedure TTabbedForm.btnGravarClick(Sender: TObject);
begin
  try
    SQLQueryInsert.ParamByName('idCons').AsInteger := PopupBoxTipo.ItemIndex;
    SQLQueryInsert.ParamByName('nome').AsString := PopupBoxTipo.Text;
    SQLQueryInsert.ParamByName('kms').AsInteger := StrToInt(kmsEdit.Text);
    SQLQueryInsert.ParamByName('quantidade').AsInteger := StrToInt(qtdEdit.Text);
    SQLQueryInsert.ParamByName('data').AsInteger := DateTimeToUnix(dtaEvento.Date);
    SQLQueryInsert.ExecSQL();
    lista.Refresh;
    LinkFillControlToField1.BindList.FillList;
  except
    on e: Exception do
    begin
      ShowMessage(e.Message);
    end;
  end;
end;

If you need some more code snippet, please ask!

Thanks in advance for any reply!

Was it helpful?

Solution

Try this method it works for me

procedure TData.InsertItem(someObject: TSomeObjectClass);
var
  qry: TFDQuery;
begin
  qry := CreateQry( 'insert into SomeObject(id, description, something)'+
  ' values (:id, :description, :something);', false);
  qry.Params.ParamByName('id').AsInteger := someObject.id;
  qry.Params.ParamByName('description').asstring := someObject.description;
  qry.Params.ParamByName('something').asstring := someObject.something;
  qry.Prepare;
  qry.execsql;
  qry.Free;
end;

I've put and object in the parameter but you can also put the data you want to insert seperatly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top