Solved

ADOQuery posting to Database immediately

Posted on 2004-09-17
19
1,561 Views
Last Modified: 2012-06-27
Hiya experts,
This is probably a simple solution, but I'm teaching myself Delphi so I'm somewhat new to it. I've got a MSAccess database where I'm saving data to. No problem here. In my application, I'm filling up a grid (from devExpress) with data by bounding it to a ADOQuery object and inserting directly into the query. The problem is, when I do the following:

qry.Insert;
qry.FieldByName('column').value := someVal;

it updates the database immediately. Any way to prevent this?

B.D.
0
Comment
Question by:Big Monty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 6
19 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12086615
> qry.Insert;
> qry.FieldByName('column').value := someVal;

> it updates the database immediately. Any way to prevent this?

I can not believe that. To be updated it must to be:
qry.Post;

I will test that and will post a new comment after that ....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12086830
I already tested your case and I'm sure that after:

  ADOQuery.Insert;
  ADOQuery.FieldByName('FIRST_NAME').Value := 'Emil';

the database is not updated immediately !

May be you use the event:
procedure TForm1.ADOQueryNewRecord(DataSet: TDataSet);
begin
  ADOQuery.FieldByName('ID').Value := Last_ID + 1;
  ADOQuery.Post;
end;
0
 
LVL 33

Author Comment

by:Big Monty
ID: 12087047
thats what i assumed i had to use to commit to the DB, qry.Post. But i kid you not that i don't have that line anywhere in my code. is there a property that automatically posts after you insert a record?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:esoftbg
ID: 12087132
There is not a property that automatically posts after you insert a record ....
There is events like:
procedure TForm1.ADOQueryNewRecord(DataSet: TDataSet);
begin
//........
end;

procedure TForm1.ADOQueryBeforeInsert(DataSet: TDataSet);
begin
//........
end;

procedure TForm1.ADOQueryAfterInsert(DataSet: TDataSet);
begin
  ADOQuery.FieldByName('MIDDLE_NAME').Value := 'S.';
  ADOQuery.Post;
end;

where is possible to be Posted a record in mistake
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12087295
But a post could be fired indirectly by DBGrid when the user changes the row wich contains the new record ....
A post could be fired by DBNavigator ....
In these cases there is not:
  ADOQuery.Post;
0
 
LVL 33

Author Comment

by:Big Monty
ID: 12087310
i'll check the grid settings when i get home tonight and see if thats whats causing the problem, although i think i tried it without the grid
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12087369
I don't mean the DBGrid settings, but the user activity with DBGrid when ADOQuery is in Insert mode:
If the user presses the down-arrow or the up-arrow keyboard keys, or clicks on another DBGrid row (not the current row where is the new inserted value) it fires internal Post for ADOQuery.
0
 
LVL 33

Author Comment

by:Big Monty
ID: 12108927
below is the code i'm using. i commented out the grid population mechanism so I know thats not it. is it because of the looping? is there a way around it?

procedure tform1.getalldir(s:string);
var rec :tsearchrec;
folderName :string;
index: integer;
begin
qry.Open;

index := 0;
if sysutils.findfirst(s + '*.*', faAnyFile, Rec)=0 then  begin
   repeat
     if (rec.name<>'.') and (rec.name<>'..') then
     begin
 //    gridView.DataController.FocusedRowIndex := index;
      folderName := Copy(rec.Name, pos('dmb', rec.Name), 13);
      qry.Insert;
     qry.FieldByName('showdate').Value := folderName;
     end;
     index := index+ 1;
   until SysUtils.FindNext(Rec) <> 0;
  sysutils.FindClose(rec);
end;
end;

B.D.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12111384
I tested your code: You are absolutely right ! All the Inserted records are updated into the database .... It is very strange it is done without qry.Post ????
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12111409
But I do not understand why You do qry.Insert, but do not want to update the Table ???? If you don't want to update the Table, then do not do qry.Insert !!!!
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12111488
You can store the FileNames from the Directory search into TStringList and when really want to insert them into the Table to do that ....

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringList := TStringList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  StringList.Free;
end;

procedure tform1.getalldir(s:string);
  var
    rec :tsearchrec;
    folderName :string;
    index: integer;
begin
  qry.Open;
  index := 0;
  if sysutils.findfirst(s + '*.*', faAnyFile, Rec)=0 then  begin
  repeat
     if (rec.name<>'.') and (rec.name<>'..') then
     begin
 //    gridView.DataController.FocusedRowIndex := index;
       folderName := Copy(rec.Name, pos('dmb', rec.Name), 13);
       StringList.Add(folderName);
       {
       qry.Insert;
       qry.FieldByName('showdate').Value := folderName;
       }
       end;
       index := index+ 1;
     until SysUtils.FindNext(Rec) <> 0;
    sysutils.FindClose(rec);
  end;
end;
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12112048
Forgot to mention about the declaration of the StringList

type
  TForm1 = class(TForm)
    //........
    private{ Private declarations }
    public { Public declarations }
      StringList:TStringList;
      procedure GetAlldir(S: string);
  end;
0
 
LVL 33

Author Comment

by:Big Monty
ID: 12112741
well, let me explain what I'm designing here. basically its a program that scans a directory for the user for music folders, then it will populate a grid. the grid i'm using is tied to a datasource, so i chose a query object to use. The only time i want to post to the database is when the user clicks the save button. the grid i'm using only populates when the datasource is populated. does that make any sense? :)

thx for the help
B.D.
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 125 total points
ID: 12112768
There is another way using Transaction, but I don't like it ....
Anyway here it is:

procedure TForm1.GetAlldir(S: string);
var
  rec:           tsearchrec;
  folderName:    string;
//  index:         integer;
begin
//  index := 0;
  if sysutils.findfirst(S + '*.*', faAnyFile, Rec)=0 then
  try
    if not ADOConnection.InTransaction then
      ADOConnection.BeginTrans;
    repeat
      if (rec.name<>'.') and (rec.name<>'..') then
      begin
 //     gridView.DataController.FocusedRowIndex := index;

       folderName := Copy(rec.Name, pos('dmb', rec.Name), 13);
       StringList.Add(folderName);
       qry.Append;
       qry.FieldByName('USER_NAME').Value := folderName;
     end;
//     index := index+ 1;
    until SysUtils.FindNext(Rec) <> 0;
    sysutils.FindClose(rec);
  finally
    ListBox.Items.Assign(StringList);
  end;
end;


procedure TForm1.btnAppendClick(Sender: TObject);
begin
  GetAlldir('');
end;

procedure TForm1.btnCommitClick(Sender: TObject);
begin
  if ADOConnection.InTransaction then
  try
    if (qry.State in [dsInsert,dsEdit]) then
      qry.Post;
  finally
    ADOConnection.CommitTrans;
  end;
end;

procedure TForm1.btnRollBackClick(Sender: TObject);
var
  SID:    string;
begin
  if ADOConnection.InTransaction then
  begin
    SID := qry.FieldByName('ID').AsString;
    ADOConnection.RollbackTrans;
    qry.Active := False;
    qry.Active := True;
    qry.Locate('ID', SID, []);
  end;
end;
0
 
LVL 33

Author Comment

by:Big Monty
ID: 12112781
why dont you like it if i may ask?
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12112936
I don't like it because the Table usually has primary key which is an autoincrement field. This way every time there are inserted new records the field ID acquires new values, but even you press the button for RollBack theese values never be available for use ....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12113437
I think the solution with a TStringList is a better one ....
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12149500
Using a TStringList you may visualize it on a TListBox instead of TDBGrid. But when you decide to post data from TStringList into Database by clicking some Button, just should do a loop:

procedure TForm1.btnPostClick(Sender: TObject);
var
  I:      Integer;
begin
  for I := 0 to StringList.Count-1 do
  begin
    qry.Insert;
    qry.FieldByName('column').AsString := StringList[I];
    qry.Post;
  end;
end;
0
 
LVL 33

Author Comment

by:Big Monty
ID: 12169654
my apologies for not closing this sooner, I've been away all of last week.

This worked so for now I'm going to use it. I don't care too much about the auto numbers not being re-usable, this is more of a personal project than anything.

Cheers
B.D.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question