Package: datalite
Datalite[!WARNING]This project is a hobby project and it should not be used for security-critical or user facing applications.It should be noted that Datalite is not suitable for secure web applications, it really is only suitable for cases when you can trust user input.Datalite is a simple Python
package that binds your dataclasses to a table in a sqlite3 database,
using it is extremely simple, say that you have a dataclass definition,
just add the decorator@datalite(db_name="db.db")to the top of the
definition, and the dataclass will now be bound to the filedb.dbDetailed API referenceDownload and InstallYou can installdatalitesimply bypipinstalldataliteOr you can clone the repository and runpythonsetup.pyDatalite has no dependencies! As it is built on Python 3.7+ standard library. Albeit, its tests requireunittestlibrary.Datalite in Actionfromdataclassesimportdataclassfromdataliteimportdatalite@datalite(db_path="db.db")@dataclassclassStudent:student_id:intstudent_name:str="John Smith"This snippet will generate a table in the sqlite3 database filedb.dbwith
table namestudentand rowsstudent_id,student_namewith datatypes
integer and text, respectively. The default value forstudent_nameisJohn Smith.Basic UsageEntry manipulationAfter creating an object traditionally, given that you used thedatalitedecorator,
the object has three new methods:.create_entry(),.update_entry()and.remove_entry(), you can add the object to its associated table
using the former, and remove it using the later. You can also update a record using
the middle.student=Student(10,"Albert Einstein")student.create_entry()# Adds the entry to the table associated in db.db.student.student_id=20# Update an object on memory.student.update_entry()# Update the corresponding record in the database.student.remove_entry()# Removes from the table.But what if you have created your object in a previous session, or wish
to remove an object unreachable? ie: If the object is already garbage
collected by the Python interpreter?remove_from(class_, obj_id)is
a function that can be used for this express purpose, for instance:remove_from(Student,2)# Removes the Student with obj_id 2.Object IDs are auto-incremented, and correspond to the order the entry were
inserted onto the system.Fetching Records:warning:Limitation! Fetch can only fetch limited classes correctly: int, float, bytes and str!Finally, you may wish to recreate objects from a table that already exist, for
this purpose we have the modulefetchmodule, from this you can importfetch_from(class_, obj_id)as well asis_fetchable(className, object_id)former fetches a record from the SQL database given its unique object_id
whereas the latter checks if it is fetchable (most likely to check if it exists.)>>>fetch_from(Student,2)Student(student_id=10,student_name='Albert Einstein')We also have four helper methods,fetch_range(class_, range_)andfetch_all(class_)are very similar: the former fetches the records
fetchable from the object id range provided by the user, whereas the
latter fetches all records. Both return a tuple ofclass_objects.The last two helper methods,fetch_if(class_, condition)fetches all
the records of typeclass_that fit a certain condition. Here conditions
must be written is SQL syntax. For easier, only one conditional checks, there
isfetch_equals(class_, field, value)that checks the value of only onefieldand returns the object whosefieldequals the providedvalue.Paginationdatalitealso supports pagination onfetch_if,fetch_allandfetch_where,
you can specifypagenumber andelement_countfor each page (default 10), for
these functions in order to get a subgroup of records.