Forums


Aducom Software :: Forums :: Delphi SQLite Version 3.0.x Components :: Questions
 
<< Voorgaande discussie | Volgende discussie >>
[newbie] How to update DB and refresh DB grid?
Ga naar pagina  [1] 2 3
Moderators: aducom, aducomadmin2
Auteur Bericht
gilles.g
di aug 05 2008, 06:42
Geregistreerde deelnemer #862
Geregistreerd: ma apr 30 2007, 10:02
berichten: 84
Hello

I need to write a little application to save a list of books into a database through their ISBN. I'd like to use a DB-aware grid object for this:

link

How can I use a DBgrid so that I can send an INSERT or UPDATE to the DB, and have the grid refresh itself automagically?

It seems like a Query object is used to send a SELECT to the database object, while changes must go through an UpdateSQL object, but I'm not clear at how it works. Could someone share a basic sample on how it works?

Thank you.
Terug omhoog
aducom
wo aug 06 2008, 04:08

Geregistreerde deelnemer #1
Geregistreerd: wo jan 25 2006, 04:34
Woonplaats: groningen (NL)
berichten: 1561
It should do this if you use a refresh on the query object. I haven't checked, it's an old app, but as far as I remember you find a sample in :

link

Again it's old and might not be compilable, but it's a start.

albert
Terug omhoog
gilles.g
wo aug 06 2008, 06:39
Geregistreerde deelnemer #862
Geregistreerd: ma apr 30 2007, 10:02
berichten: 84
Thanks for the sample, I'll check it out.

Does it mean that people don't use Aducom's Query/UpdateSQL objects to work with SQLite, and rely on something else (ADO, dbExpress)?
Terug omhoog
aducom
do aug 07 2008, 02:16

Geregistreerde deelnemer #1
Geregistreerd: wo jan 25 2006, 04:34
Woonplaats: groningen (NL)
berichten: 1561
AFAIK updatesql is used a lot and actually it is based upon the documentation of the VCL. So any sample of the VCL should work. Fact is that I don't have all kind of samples to work with the components.

albert
Terug omhoog
gilles.g
vr aug 08 2008, 11:34
Geregistreerde deelnemer #862
Geregistreerd: ma apr 30 2007, 10:02
berichten: 84
Thanks for the help. I'm reading Bob Swart's book on Delphi + Databases, but don't find the following property in the ASQLite3Query object:

"Note the syntax for the parameter: a colon specifies that this is a parameter name
(usually, but not necessarily with the same name as the column it needs to connect to).
After we’ve specified the SQL contents, we need to specify the DataType and ParamType
properties of the parameter. Double-click on the Params property of the qBorrowFriend
TQuery command to get the Parameters Editor.
Figure 7. TQuery Parameters Editors
For each parameter – in this example only one of course – we must set the ParamType to
input or output (ptInput in this case) and the DataType to the corresponding column type
(ftInteger in this case). It may also be useful to specify a default value and type (set to
Integer and 0 for example), so you can open the qBorrowFriend at design-time if you
wish."

Is it the equivalent of double-clicking on the object, right-clicking in the dialog, and choosing "New Field..."?
Terug omhoog
aducom
vr aug 08 2008, 02:38

Geregistreerde deelnemer #1
Geregistreerd: wo jan 25 2006, 04:34
Woonplaats: groningen (NL)
berichten: 1561
I realy don't understand your question. The Params property is there:
with MyQuery do begin
   sql.text := 'select * from xxx where a=:a';
    params[0].AsString := 'something';
    open;
    while not eof do begin
              x := FieldByName('something').AsString;
              next;
    end;
end;


[ Bijgewerkt vr aug 08 2008, 02:40 ]

albert
Terug omhoog
gilles.g
za aug 09 2008, 04:12
Geregistreerde deelnemer #862
Geregistreerd: ma apr 30 2007, 10:02
berichten: 84
Sorry, I was looking for a Params property for a ASQLite3Query object in the Object Inspector.

One last question, though: If I can perform updates + refresh through a Query objct, when should I use an UpdateSQL object? I don't understand why Borland provided two objects.

FWIW, depending on applications, I'll use data-aware grids or non-data-aware grids.

Here's an example of what I'd like to do (use a Query to display a SELECT into a DBgrid, and let the user add a record by pushing a button):


procedure TForm1.Button1Click(Sender: TObject);
begin
  With ASQLite3Query1 do begin
    //Close;
    SQL.Text := 'INSERT INTO books (isbn,language) VALUES (:a,:b)';
    params[0].AsInteger := 234;
    params[1].AsString := 'something';

    //Exception class EDatabaseError with message 'SQL logic error or missing database'.
    //ExecSQL;

    //Open;
  end;

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)');
    //SQLite3_ExecSQL('INSERT INTO books (isbn,language) VALUES (123,"english")');
  end;

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

  DataSource1.DataSet := ASQLite3Query1;

  With NextDBGrid1 do begin
    DataSource := DataSource1;
    //Have the right-most column fill remaining space
    Columns[ASQLite3Query1.FieldCount-1].Options := NextDBGrid1.Columns[ASQLite3Query1.FieldCount-1].Options + [coAutoSize];
  end;

end;


Thank you.

[ Bijgewerkt za aug 09 2008, 04:30 ]
Terug omhoog
gilles.g
za aug 09 2008, 06:55
Geregistreerde deelnemer #862
Geregistreerd: ma apr 30 2007, 10:02
berichten: 84
I'm going through the samples, and was wondering 1) why there's a need for two Query objects, each pointing to a shared UpdateSQL object, and 2) why the INSERT string is mentionned twice, in the Query and in the UpdateSQL:


  object ASQLite3UpdateSQL1: TASQLite3UpdateSQL
    InsertSQL.Strings = (
      'insert into animal *')
  end
[...]
procedure TForm1.Button1Click(Sender: TObject);
begin
 with ASQLite3Query2 do begin
   Close;
   SQL.Clear;
   SQL.Add('insert into animal (id, desc) values (:v1, :v2)');
   Params[0].AsString := '99';
   Params[1].AsString := 'ninetynine';
   ExecSQL;
 end;
end;


Thank you.
Terug omhoog
gilles.g
za aug 09 2008, 07:04
Geregistreerde deelnemer #862
Geregistreerd: ma apr 30 2007, 10:02
berichten: 84
Here's the latest:


procedure TForm1.FormActivate(Sender: TObject);
begin
    //1. Create database and table
  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;

    //2. Configure update
  With ASQLite3UpdateSQL1 do begin
    InsertSQL.Add('INSERT INTO books *');
  end;

    //3. Configure SELECT, and link query to update object
  With ASQLite3Query1 do begin
    Connection := ASQLite3DB1;
    SQL.Text := 'SELECT * FROM books';
    UpdateSQL := ASQLite3UpdateSQL1;
    Open;
  end;

    //4. Glue to connect DBgrid to dataset
  DataSource1.DataSet := ASQLite3Query1;

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

end;

procedure TForm1.Button1Click(Sender: TObject);
var
  InsertData : TStrings;
begin
    //6. Add record
 with ASQLite3Query1 do begin
   Close;
   SQL.Clear;
   SQL.Add('INSERT INTO books (isbn,language) VALUES (NULL,:a)');
   Params[0].AsString := 'test';
        //Does add record, but raises error afterward
        //Project1.exe raised exception class EAccessViolation with message 'Access violation at address 004DB408
   ExecSQL;
 end;
end;

end.


Thank you.
Terug omhoog
aducom
za aug 09 2008, 07:08

Geregistreerde deelnemer #1
Geregistreerd: wo jan 25 2006, 04:34
Woonplaats: groningen (NL)
berichten: 1561
The UpdateSQL is used to store the three sqlstatements needed for genreting a live query on data-aware components. Since the query is copied into the sql statement, the select is lost and need to be refreshed.

albert
Terug omhoog
Ga naar pagina  [1] 2 3  

Ga naar:     Terug omhoog

Publiceer deze discussie: RSS 0.92 Publiceer deze discussie: RSS 2.0 Publiceer deze discussie: RDF
Powered by e107 Forum System