Package: datalite3
datalite3It 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("mydb.db")to the top of the
definition, and the dataclass will now be bound to the filemydb.dbDetailed API referenceDownload and InstallYou can installdatalite3simply bypipinstalldatalite3Or you can clone the repository and runpythonsetup.pyDatalite has no dependencies! As it is built on Python 3.6+ standard library. Albeit, its tests requireunittestlibrary.Datalite in Actionfromdataclassesimportdataclassfromdatalite3importdatalite@datalite(db="mydb.db")@dataclassclassStudent:student_id:intstudent_name:str="John Smith"This snippet will generate a table in the sqlite3 database filemydb.dbwith
table namestudentand columnsstudent_id,student_namewith SQL typesINTEGERandTEXT, respectively. The default value forstudent_nameisJohn Smith. A third column__id__will be automatically added to hold the
primary key for the records.##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.Custom primary keyIf you we want to declare one or more of the fields as part of the primary key for our
records, we can just annotate them withPrimary[type], wheretypecan be one ofint,float,str,bytes.fromdataclassesimportdataclassfromdatalite3importdatalite,Primary@datalite(db="mydb.db")@dataclassclassStudent:student_id:Primary[int]student_name:str="John Smith"This snippet will generate a table in the sqlite3 database filemydb.dbwith
table namestudentand columnsstudent_id,student_namewith SQL typesINTEGERandTEXT, respectively. The columnstudent_idwill be also the primary
key and no__id__column will be added.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 exists, for
this purpose we have thefetchmodule, from this you can importfetch_from(class_, key)as well asis_fetchable(className, key)former fetches a record from the SQL database given its unique key
whereas the latter checks if it is fetchable (most likely to check if it exists.)>>>fetch_from(Student,20)Student(student_id=20,student_name='Albert Einstein')We also have three helper methods:fetch_all(class_): fetches all records of a given class. Returns a tuple ofclass_objects;fetch_if(class_, condition): fetches all the records of typeclass_that satisfy the given
condition. Here conditions must be written is SQL syntax;fetch_equals(class_, field, value): fetches all the records of typeclass_that have the
columnfieldset tovalue;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.