Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 08-09-2008, 02:30 PM
fredtheman fredtheman is offline
Member
 
Join Date: Dec 2004
Posts: 52
Default [newbie] DBgrid + updating a database?

Hello

I'm using a BDE-like Delphi wrapper to SQLite, and a DBgrid to display the dataset from a TQuery object.

I have two questions:
1. Do I really need two TQuery and one TUpdateSQL to perform updates, or could I somehow get away with just one TQuery (and possibly no TUpdateSQL)?

2. How does the TUpdateSQL object get the parameters from the second TQuery, since its InsertSQL.Text is generic ("INSERT INTO books *")?

Here's the code that I came up with, which does work:

Code:
procedure TForm1.Button1Click(Sender: TObject);
begin
 with ASQLite3Query2 do begin
   Close;
   SQL.Clear;
   SQL.Text := 'INSERT INTO books (isbn,language) VALUES (NULL,:a)';
   Params[0].AsString := 'test';
   ExecSQL;
 end;

  ASQLite3Query1.Refresh; 
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  With ASQLite3DB1 do begin
    DefaultDir := ExtractFileDir(Application.ExeName);
    Database := 'mybooks.sqlite';
    Open;
    SQLite3_ExecSQL('CREATE TABLE IF NOT EXISTS books (isbn INTEGER PRIMARY KEY, language VARCHAR)');
  end;

  With ASQLite3UpdateSQL1 do begin
    InsertSQL.Text := 'INSERT INTO books *';
  end;

  With ASQLite3Query1 do begin
    Connection := ASQLite3DB1;
    SQL.Text := 'SELECT * FROM books';
    UpdateSQL := ASQLite3UpdateSQL1;
    Open;
  end;

  With ASQLite3Query2 do begin
    Connection := ASQLite3DB1;
    SQL.Text := 'SELECT * FROM books';
    UpdateSQL := ASQLite3UpdateSQL1;
    Open;
  end;

  DataSource1.DataSet := ASQLite3Query1;

  With NextDBGrid1 do begin
    DataSource := DataSource1;
    Columns[ASQLite3Query1.FieldCount-1].Options := NextDBGrid1.Columns[ASQLite3Query1.FieldCount-1].Options + [coAutoSize];
  end;

end;
Thank you.
Reply With Quote
  #2  
Old 08-11-2008, 03:40 AM
mshkolnik mshkolnik is offline
Senior Member
 
Join Date: Jul 2001
Posts: 4,195
Default RE: [newbie] DBgrid + updating a database?

1. you need 1 TQuery only. With sql-statment (SELECT * FROM books)
2. link your TUpdateSQL to TQuery (see TUpdateObject property) and write (or generate automatically) the INSERT/UPDATE/DELETE statements for TUpdateSQL

Nothing more - all will work automatically without any your programming. When you'll edit the data in query (dbgrid), these changes will be sent to db-server


With best regards, Mike Shkolnik
http://www.scalabium.com
Reply With Quote
  #3  
Old 08-17-2008, 02:15 PM
fredtheman fredtheman is offline
Member
 
Join Date: Dec 2004
Posts: 52
Default RE: [newbie] DBgrid + updating a database?

Thanks Mike. Turns out I just needed a couple of instructions to get the TQuery/TUpdateSQL combo to work:

Code:
  With ASQLite3UpdateSQL1 do begin
    InsertSQL.Text := 'INSERT INTO books *';
    UpdateSQL.Text := 'UPDATE books SET title=:title WHERE isbn=:isbn';
  end;

  With ASQLite3Query1 do begin
    Connection := ASQLite3DB1;
    SQL.Text := 'SELECT * FROM books';
    Open;
    UpdateSQL := ASQLite3UpdateSQL1;
  end;
[...]
  With ASQLite3Query1 do begin
    //To UPDATE a record
    Append;
    FieldByName('isbn').AsString := LabeledEdit1.Text;
    FieldByName('language').AsString := Radiogroup1.Items.Strings[RadioGroup1.ItemIndex];
    Post;

    //To INSERT a record
    Edit;
    FieldByName('title').AsString :=   PerlRegEx1.SubExpressions[1];
    Post;
  end;
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:49 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.