The db moduleο
The db module contains the DBConnection class and the Profiles database table
The Database Table
In the Profiles database table each row corresponds to a unique Profile
- The table only has two fields per row:
- name: primary key for lookups/insertions
- config: stores the Profile as pickle bytes via- to_pickle()
 
How is profile data saved to the database?
When a Profile calls save() and has local = False, it will
connect() to the database specified by the DABATASE_URL environment variable and use it
to query_profile() settings
- If the - profile_exists()in the database already, its- configdata will be updated
- Otherwise, the - DBConnectionwill- save_profile()data in a new table row
Important!!
You MUST configure the DATABASE_URL environment variable to save/load remotely
- InstaTweet uses - SQLAlchemyto create a- DBConnectionβ any db it supports is compatible
- See the - dbmodule for more information
One Last Thing!
The DBConnection is meant to be used as a context manager
with DBConnection() as db:
    # Do Something
- A - SESSIONis created/destroyed when saving, loading, and InstaTweeting a- Profile
If you donβt want that, hereβs instructions on Persisting The DBConnection
β¦
- InstaTweet.db.DATABASE_URLο
- The Database URL to use, obtained from the - DATABASE_URLenvironment variable
β¦
- class InstaTweet.db.Profiles(**kwargs)[source]View on GitHubο
- Database table used for storing - Profilesettings- The table currently has only 2 fields, for the - nameand pickle bytes of the profile- configο
- The pickle bytes from - Profile.to_pickle()
 
β¦
- class InstaTweet.db.DBConnection[source]View on GitHubο
- Database Connection class with context management ooh wow - Uses - SQLAlchemyto connect and interact with the database specified by- DATABASE_URLenvironment variable- Sample Usage: - def poop_check(): with DBConnection() as db: if db.query_profile(name="POOP").first(): raise FileExistsError('DELETE THIS NEPHEW......') - SESSIONο
- The currently active session; closed on object exit - Type
 
 - ENGINEο
- The engine for the currently set - DATABASE_URL; reused after first connection- Type
 
 - static connect()[source]View on GitHubο
- Creates a - scoped_sessionand assigns it to- DBConnection.SESSION
 - query_profile(name)[source]View on GitHubο
- Queries the database for a - Profileby its name- Parameters
- name (str) β the profile name (ie. the - Profile.name)
- Returns
- Return type
 
 - load_profile(name)[source]View on GitHubο
- Loads a profile from the database by name - Parameters
- name (str) β the profile name (ie. the - Profile.name)
- Raises
- LookupError β if the database has no profile saved with the specified name 
- Return type
 
 - save_profile(profile, alert=True)[source]View on GitHubο
- Saves a - Profileto the database by either updating an existing row or inserting a new one
 - delete_profile(name, alert=True)[source]View on GitHubο
- Deletes a - Profilefrom the database by name- Parameters
- name (str) β the profile name (ie. the - Profile.name)
- alert (bool) β if - True, will print a message upon successfully deleting
 
- Return type