Solved

ADOQuery posting to Database immediately

Posted on 2004-09-17
19
1,478 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
  • 13
  • 6
19 Comments
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
> 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
Comment Utility
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 32

Author Comment

by:Big Monty
Comment Utility
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
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
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
Comment Utility
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 32

Author Comment

by:Big Monty
Comment Utility
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
Comment Utility
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 32

Author Comment

by:Big Monty
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
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
Comment Utility
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
Comment Utility
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 32

Author Comment

by:Big Monty
Comment Utility
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
Comment Utility
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 32

Author Comment

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

Expert Comment

by:esoftbg
Comment Utility
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
Comment Utility
I think the solution with a TStringList is a better one ....
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
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 32

Author Comment

by:Big Monty
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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 this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now