Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
This package removes all the annoying boilerplate code you have to write when using sqlite3
. Instead, use the methods this package provides, and easily manage your sqlite database in your python project.
For more information, check out the PyPi page
To install the newest version, use:
pip install sqlite-middleware
All the functionality of this package is in the Db
class.
When creating a new instance of this class, you should specify following parameters:
Example usage:
db = Db("demo.db")
When an instance of Db
is deleted, the __del__
magic method will close the connection.
Here are all the methods that the Db
class provides:
This method will retrieve and return all objects of a certain class from the sqlite database.
Parameters:
Example usage:
db.select_all(Person)
This method will retrieve and return an object with certain id and class from the database.
Parameters:
Example usage:
db.select_by_id(Person, 2)
This method will retrieve and return an object with certain value for a certain attribute
Parameters:
Example usage:
db.select_by_custom_attribute(Person, "name", "John Doe")
This method will create a table in the database for objects of a certain class.
Parameters:
Example usage:
db.create_table(Person())
This method will save an object in the database.
It will also create a table if it doesn't exist yet.
Parameters:
Example usage:
person = Person("John Doe")
db.save_object(person)
This method will update an existing object in the database.
Parameters:
Example usage:
person = Person("John Doe")
db.save_object(person) # Say this is the first person, and gets id 1
person.set_name("Michael Myers")
db.update_object(1, person)
This method will delete an object from the database.
Parameters:
Example usage:
person = Person("John Doe")
db.save_object(person) # Say this is the first person, and gets id 1
db.delete_object(Person, 1)
By default table names are created with the name of the class you specified.
tbl[Classname]s
The reason that we must pass an object, and not a class, to db.create_table()
, is because it takes all the instance variables of this object and uses them to create a column in the sqlite table.
For example with a class named Person
, which has a name
parameter:
db.create_table(Person())
The line above would create a table, with name tblPersons
and it would have 2 columns name
, and also an auto-generated column for id.
As we need id's to specify what object to delte, the id needs to be stored in the database, this is done automatically when creating a table / saving an object.
FAQs
This is a python package that makes it easier to work with sqlite
We found that sqlite-middleware demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.