Recent

Author Topic: [Solved]Dbgrid add new records from query without loss of old data  (Read 8975 times)

doniuzas

  • New Member
  • *
  • Posts: 13
hello, please help.
i have this structure zconnection1->zquery1->datasource1->dbgrid1
How i can add additional data from query on button click without loss old data?
I know i need add some like a memodataset or bufdataset or jvmemorydata, but how whole code?
my query code on button click is:

zquery1.sql.clear;
zquery1.Params.CreateParam(ftstring,'someparam',ptunknown);
zquery1.params.ParamValues['someparam']:=dbtext1.field.asstring;
zquery1.sql.add('select title from table where x= :someparam');
zquery1.active:=true;

thank you in advance for your answers.
« Last Edit: February 11, 2015, 09:18:07 am by doniuzas »

doniuzas

  • New Member
  • *
  • Posts: 13
Re: Dbgrid add new records from query without loss of old data
« Reply #1 on: February 03, 2015, 06:52:12 pm »
no answers?  :(

kapibara

  • Hero Member
  • *****
  • Posts: 649
Re: Dbgrid add new records from query without loss of old data
« Reply #2 on: February 03, 2015, 07:27:36 pm »
doniuzas, the DBGrid doesn't show anything except whats in the query. So you actually never add or delete data directly from a DBGrid. It is just a mirror of the content in its associated SQLQuery. If you want to add content to the DBGrid then you do it indirectly, by modifying the SQL select statement. The data then simply shows up in the grid without you having to do anything.

Its not clear to me exactly what you are doing, but perhaps you could have a button that closes the current query, assigns a new select statement and re-opens the query. If you need to keep your position in the grid, then run the Locate command after re-opening the query.

If you explain a bit more, maybe we can come up with another answer.

Also, you dont need to create the Param if you assign the select statement first:

Code: [Select]
zquery1.SQL.Text:= 'SELECT title FROM table WHERE x= :someparam'
The select statement doesnt need to be cleared when you assign to SQL.Text.
And then you can just use the param, it has been created for you automatically:

Code: [Select]
zquery1.ParamByName('someparam').asstring:= dbtext1.field.asstring;
zquery.Open;

Lazarus trunk / fpc 3.2.2 / Kubuntu 24.04 - 64 bit

doniuzas

  • New Member
  • *
  • Posts: 13
Re: Dbgrid add new records from query without loss of old data
« Reply #3 on: February 03, 2015, 07:50:17 pm »
i can make zquery.Open; zquery.close and re-opening the query but i need make like a virtual table or someting for save previously assigned select statment results and show in dbgrid.

my schema is:
zconn1->zquery1->dbgrid1
dbtext1.field.asstring give me string parameter from dbgrid1 (zquery2.ParamByName('someparam').asstring:= dbtext1.field.asstring;)

zconn1->zquery2->dbgrid2
zquery2.SQL.Text:= 'SELECT title FROM table WHERE x= :someparam'
i need make someting like this:
zconn1->zquery2-> virtual table or virtual dataset(for adding executed zquery2 statments results)->dbgrid2

balazsszekely

  • Guest
Re: Dbgrid add new records from query without loss of old data
« Reply #4 on: February 03, 2015, 08:56:41 pm »
Still not clear what are you after! Lets say you have a table: POEPLE, with the following fields ID, NAME, AGE(integer, varchar(32), integer)
ID     NAME    AGE
1       AAA      25
2       BBB      43
3       CCC     15
4       DDD     64 
5       FFF       22

zconn1->zquery1->dbgrid1
Your zquery1 SQL looks like this: select * from PEOPLE where age <= :pAge
Code: [Select]
//...
zquery1.ParamByName('pAge').AsInteger := 25
zquery.Open;
//..
Now your grid will be populated with the following values:
1       AAA      25
3       CCC     15
5       FFF       22

Do you wish to add more data to table People, with the possibility to show the current configuration at later time? Please be more specific.
Try do describe it with a few example. How table people should look like, what do you want to see in the grid, etc...

doniuzas

  • New Member
  • *
  • Posts: 13
Re: Dbgrid add new records from query without loss of old data
« Reply #5 on: February 03, 2015, 09:57:37 pm »
OK with this example I have database with two tables, first is people(names,age), second is details(age, adress). from first query  I get results with age <=25 and populated to dbgrid1, now from dbtext.field.as string (pointed to dbgrid1) I'm get parameter for second query (query2). query2 pointed to dbgrid2. now if I execute query2 with param 25 I'm see in dbgrid2 25 and adress1, I need make second query2 with param 15 but need keep populated info in dbgrid2 (25 and adress1).
Need something like virtual table(keep info in memory) between query2 and dbgrid2.
Query2 inserting(updating) into virtual table(dataset) and this virtual table is pointed to dbgrid2. But how this make? need full code.
may by now its clear? :)

doniuzas

  • New Member
  • *
  • Posts: 13
Re: Dbgrid add new records from query without loss of old data
« Reply #6 on: February 03, 2015, 10:01:30 pm »
... if its not clear... I have two query components and two dbgrid components dropped in to form... :)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Dbgrid add new records from query without loss of old data
« Reply #7 on: February 05, 2015, 09:27:24 am »
I'll try to undersand why you want old data on screen, but if you look at my unit, you can find a way to create a memorytable (TBufdataset). Just rename it to .pas file

I think it's better to use a master / detail relation of the two tables.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

doniuzas

  • New Member
  • *
  • Posts: 13
Re: Dbgrid add new records from query without loss of old data
« Reply #8 on: February 05, 2015, 11:46:56 am »
why old data on screen need?
ok this is application for collecting data from database with different statments and all this "data pack" print as barcode(prices) labels. this apps is for two big supermarkets.
« Last Edit: February 05, 2015, 11:51:49 am by doniuzas »

doniuzas

  • New Member
  • *
  • Posts: 13
Re: Dbgrid add new records from query without loss of old data
« Reply #9 on: February 05, 2015, 11:56:51 am »
sorry mayby im noob, but i not understand your unit, for my is too hard  :(

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Dbgrid add new records from query without loss of old data
« Reply #10 on: February 05, 2015, 01:17:41 pm »
Wait. Your developping for two big supermarkets and you can't understand my (very) simple class code?
This is what you need for memorytable (TBufdataset)
Code: [Select]
constructor TGlobals.Create;
begin
  inherited;
    ClDataSet := TBufDataset.Create(nil);
  with ClDataSet do
  begin
    FieldDefs.Add('field1',ftString, 50);
    FieldDefs.Add('field2',ftString, 50);
  end;
  ClDataset.CreateDataset;
  ClDataSet.Active := true;
end;
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

doniuzas

  • New Member
  • *
  • Posts: 13
Re: Dbgrid add new records from query without loss of old data
« Reply #11 on: February 05, 2015, 02:03:48 pm »
im not asking how create bufdataset, i need code sample how from query to bufdataset replace info (insert,copy,append or etc.)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Dbgrid add new records from query without loss of old data
« Reply #12 on: February 05, 2015, 02:47:28 pm »
TBufdataset is not like TClientdataset in Delphi.
Quote
I know i need add some like a memodataset or bufdataset or jvmemorydata, but how whole code?
Code: [Select]
procedure AddSomethingToMemtable(aDataset : TDataset);
var index : integer;
begin
  while not aDataset.eof do
  begin
      for index := 0 to aDataset.fieldcount - 1 do
        Bufdataset[index].Value := aDataset[index].Value;
  end;
end;
Jus behave tbufdataset as a normal TDataset
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018