Hi ,
Could you please post here a piece of very simple code to demonstrate how to bind sqlite3 database to a (Saving)DataBox control?Thanks.
Regards,
Sam
sqlite3 data binding to DataBox
Re: sqlite3 data binding to DataBox
Hi Sam,
First, you took a look at the MovieCollection sample already, right?
The genericEditor in that sample uses the FieldDataBox (which is a class that inherits from DataBox) which connects the DataBox to the SQLite database. This generic editor is meant to work with any schema: it will automatically let you edit all the fields in the table.
The FieldDataBox (as well as the other Field controls which inherit from it: FieldCheckButton, FieldDropDataBox and EditFieldDropDataBox) are used with the EditSection and ListSection components to automatically save and load from the database.
EditSection holds all the fields to edit, whereas ListSection lets you select the entry to modify as well as create/delete entries. By using EditSection and ListSection directly (i.e. as opposed to using them through the GenericEditor), you can have control on how to lay out your controls.
These classes are all defined in eda/gui.ec, so you can look at their implementation should you want to customize them even more. Also, the TableEditor (gui/TableEditor) is an alternate editing model still being worked on (we don't have a sample for this one yet), which works in conjunction with the FieldBox class. Smiliarly to the Edit/ListSection, it works with generic types.
Now I will try to explain the basics of connecting a plain (Saving)DataBox to a db. First, it is much simpler if you are implementing a DataBox for a specific type. You use the DataBox like you would use any data box... By settings its type and pointing it to a storage location for it. If you deal with a specific type, say an int, it's quite trivial. Here's sample code that retrieves and save an int from a DB row & field passed in:
Field(Data)Box have some fancy code to do all this generically, handling any data type, so it's a bit more complicated.
Hope this helps!
Regards,
Jerome
EDIT: Sorry, the class(int) was not necessary in there (eC automatically adds it from the type of 'intStorage')
First, you took a look at the MovieCollection sample already, right?
The genericEditor in that sample uses the FieldDataBox (which is a class that inherits from DataBox) which connects the DataBox to the SQLite database. This generic editor is meant to work with any schema: it will automatically let you edit all the fields in the table.
The FieldDataBox (as well as the other Field controls which inherit from it: FieldCheckButton, FieldDropDataBox and EditFieldDropDataBox) are used with the EditSection and ListSection components to automatically save and load from the database.
EditSection holds all the fields to edit, whereas ListSection lets you select the entry to modify as well as create/delete entries. By using EditSection and ListSection directly (i.e. as opposed to using them through the GenericEditor), you can have control on how to lay out your controls.
These classes are all defined in eda/gui.ec, so you can look at their implementation should you want to customize them even more. Also, the TableEditor (gui/TableEditor) is an alternate editing model still being worked on (we don't have a sample for this one yet), which works in conjunction with the FieldBox class. Smiliarly to the Edit/ListSection, it works with generic types.
Now I will try to explain the basics of connecting a plain (Saving)DataBox to a db. First, it is much simpler if you are implementing a DataBox for a specific type. You use the DataBox like you would use any data box... By settings its type and pointing it to a storage location for it. If you deal with a specific type, say an int, it's quite trivial. Here's sample code that retrieves and save an int from a DB row & field passed in:
Code: Select all
class MyEditor : Window
{
int intStorage;
SavingDataBox dBox { this, type = class(int), data = &intStorage };
void Load(Row row, Field field)
{
row.GetData(field, intStorage);
dBox.Refresh();
}
void Save(Row row, Field field)
{
row.SetData(field, intStorage);
}
}
Hope this helps!
Regards,
Jerome
EDIT: Sorry, the class(int) was not necessary in there (eC automatically adds it from the type of 'intStorage')