[Tutor] Help with storing a variable

ThreeBlindQuarks threesomequarks at proton.me
Wed Nov 1 11:54:31 EDT 2023


Just to be clear, Databases are often designed with helpful features if you understand them and use them properly. You can sometimes continue doing things other ways. So if you have a field in a table set to autoincrement, that can be useful but not the only way. You can also have such an incremented numeric field alongside a prefix and or suffix field that you combine into another column or create a joint key.

But if you are working from Python or some other programming language, you can simply make a column that is normal and do the calculations yourself. Your program can even read in all entries, split the text to isolate your number, find the biggest one in use, increment that and use that for a new entry you crate and then request your new record be added. Obviously, that is not the best way for larger amounts of data. 

An alternative is to create ANOTHER table in which you store the name of a variable alongside the next (or current) sequence number. You can then query that table to get the next available and after using it, store the new number in the table.

Obviously when many applications/users may be using the same data at once, many new concerns arise including locking access and race conditions and balancing resources. At some point, a database designed for this is even more of a good choice.

But as noted, for simpler uses, Python has oodles of ways to do things such as pickle and many others. Some let you create sections in a data file containing various kinds of data. Some store it in JSON format. Many methods allow you to store a data structure that contains both the rows/columns you want to save as well as additional parts like a variable containing the sequence number all at once. 

Many such methods are perfectly fine for small uses like a startup file containing a few options. Having to rewrite large amounts in such a file regularly when just an update of one record is needed or when multiple processes are updating, is not a great choice.

So use what makes sense for the needs but don't make it more complex if the needs are met more simply.





Sent with Proton Mail secure email.

------- Original Message -------
On Wednesday, November 1st, 2023 at 7:33 AM, Sibylle Koczian <nulla.epistola at web.de> wrote:


> Am 01.11.2023 um 02:48 schrieb ThreeBlindQuarks via Tutor:
> 
> > NN,
> > 
> > Your question is wide open with many possible solutions.
> > 
> > You say you are storing (presumably reading in if a specific EXCEL file exists, and the updating or rewriting it) so clearly the obvious place is the same file. This would allow portability.
> > 
> > Allen has already suggested a database for all the data as a better alternative but that could require the machine you store it on be known and have the right software.
> 
> He specifically mentioned SQLite - and for practice that sounds quite
> well. Of course for an application with possibly many users of one and
> the same database it might not be.
> 
> But tenant-codes (which sound like primary keys in a SQL context) with
> different prefixes and autoincrementing number parts would need second
> thoughts.
> 
> Regards
> Sibylle
> 
> 
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list