I made another example, without TSQLite3Connect. This uses only TSQLite3DataSet components. Database is the same Chinook.db file, which I uploaded before. I started this with the purpose of various tests, but as there have been many questions on SQLite3 database, like "changes are not stored", etc., I hope this example helps those who try to use SQLite.
First I tried to use artist and album tables, but
the album table causes problems, possibly because it has a blob field. Hope this is checked.
So I used customer and invoice tables, and name of TSQLite3DataSet for each table is tblCustomer and tblInvoice. Datasource name for each dataset is dsCustomer and dsInvoice.
These two tables are master-slave relationship. Invoices are for each customer, linked by the CustomerID field.
The key points here are with tblInvoice :
MasterCource : tblInvoice
MasterFields : CustomerID
IndexFieldNames : CustomerID When executed, the right grid, which is for tblInvoice, will show only for each employee selected at left grid, which is for tblCustomer. It's little bit weird that every customer has 7 invoices.
To modify the content of invoice table here, I set following properties of tblInvoice.
PrimaryKey : InvoiceID
AutoincrementKey: trueWith this, when I insert a record by pressing (+) button of dbnavigator, Invoice ID was filled in automatically. But not other fields like address, country, etc. And I keyed in total amount.
With this, I could modify the content at dbgrid and post, but the changes were not saved. When I go to other customers and came back, the changes were not saved. So I set
SaveOnRefresh : true
SaveOnClose: true
And then I tried again... and at this time, error message popped up. InvoiceDate field should not be null. This is strange that the field is not filled in.
Up to this, there were no typing of codes at all in unit file.
To make this simple program working, I added following codes.
procedure TForm1.tblInvoiceBeforePost(DataSet: TDataSet);
begin
tblInvoice.FieldByName('InvoiceDate').AsDateTime:= now;
tblInvoice.fieldbyName('Billingaddress').asstring := tblCustomer.FieldByName('Address').AsString;
end;
Of course I can add other fields like country, city, etc. of invoice table, copying from customer table.
And now, it seems to work, at least inserting and deleting. This seems quite simple way to manage relational database. I'd like to make a program to manage my own customers and contacts.