Android Question SQLite: [Save] or [Update-Insert]

LucaMs

Expert
Licensed User
Longtime User
This question is not specific to B4A but it is not suitable for Chit Chat!

Suppose we have a class that represents an entity (such as an inventory item).

The class contains a property for each field of a database table.

Suppose I want to update a field of a record by the primary key.

I can load the data in the class object via a method contained in the class:
B4X:
InvenItem.FillForPK (PK)
(I would also have a suggestion for the name :) - "Fill" Or "Load"? / "For", "By" or other?)


Then I set a property to be updated:
B4X:
InvenItem.Qty = NewQuantity


At this point I could have a method:
B4X:
InvenItem.Save
which, internally, checks if there is already a record containing that PK; in this case the method performs an Update, otherwise it performs an Insert.

Or I could have two methods, Update and Insert.


What you recommended me to write in the class:

a) only the Save method (which decides whether to update or insert)
b) Insert and Update methods (in which case the developer will have to decide)


Thank you in advance for your answers.
 
Last edited:

keirS

Well-Known Member
Licensed User
Longtime User
You missed out one option which is one I have used in the past. Do a delete then an insert. That way you don't need to check if the record exits.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It seems to be not so interesting: 60 visitors, only one answer :D.

Perhaps a good solution would be a method:
B4X:
Save(AllowUpdate As Boolean)

If AllowUpdate is false and the method finds a record with the same PK, the method throws an exception.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
89 now!

Maybe I should create a new statistical survey:

a) I have explained it badly;
b) although the visitors would have the same doubt;
c) they have closed the page after the first two lines.

Even Alien has not given feedback!

:D

Well, I'll do it by myself; as in the previous post.

"Who makes himself makes three" (literal translation of the Italian proverb: "Chi fa da sé fa per tre";
it could be:
"If you want something done, do it yourself"
"bien servi que par soi-même"

:)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If I understand well, the Class contains only a record of the database.
Where and how is the database managed ?
In all my programs the user has two options Update and Add for whole records not individual fields.
If the Class contains only a record of the database I don't see tany advantage using a class.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
If I understand well, the Class contains only a record of the database.
Where and how is the database managed ?
In all my programs the user has two options Update and Add for whole records not individual fields.
If the Class contains only a record of the database I don't see tany advantage using a class.

First of all, thanks for your answer, Klaus.

Indeed, just a few minutes ago, after I created the Save method inside the class, I thought that in this way you can manage only local databases (device).

As a result, I will develop a module for communication between "class" and db.

The advantages of using a class are manifold.

The first that come to mind are simplicity for the programmer:

Employee.Name = ...
If Employee.EmplDate ...

The class could contain also methods, for example to perform calculations inside the class.

Or something like:
Order.Details.xxxx - Order and Detail are classes (Details could be a property of Order - a map of Detail). (uhm... Maybe OrderItem, instead of Detail :)

I am also thinking of developing the GUI for data management.
In a scrollview (CustomListView or whatever) I could associate a class (object) to an Item (panel).
(I'm trying to create a sw which, when the user selects some tables in a db, developes both classes and GUIs. It's hard :D)


Thanks again, Klaus.
 
Last edited:
Upvote 0
Top