Package: presql
presqlPostgreSQL and Psycopg2 wrapper.Official ReleasePreSQL can now be used on your Python projects through PyPi by running pip command on a Python-ready environment.pip install presql --upgradeCurrent version is 1.0.0, but more updates are coming soon. Installing it will also install required packages includingpsycopg2.This is compatible with Python 3.9 or with the latest version.package importfrom presql import PreSQLThere are many ways to connect to a Postgres server, check Psycopg2Connectiondocumentation for more details. Some of those features may or may not be implement as of the current version.Important!As always, do not store your credentials in the project folder.Connect using a valid Database URIwithPreSQL(DATABASE_URI,sslmode="require")asdb:print("\nConnect via URI",db.connected())Connect using a valid DSN StringwithPreSQL(f"dbname={DATABASE}user={USER}password={PASSWORD}")asdb:print("\nConnect via DSN",db.connected())Connect using parameterswithPreSQL(dbname=DATABASE,user=USER,password=PASSWORD,host=HOST,port=PORT)asdb:print("\nConnect using parameters",db.connected())Manual SQL commandswithPreSQL(dbname=DATABASE,user=USER,password=PASSWORD,host=HOST,port=PORT)asdb:ifdb.connected():query="""CREATE TABLE IF NOT EXISTS inventory (inventory_id integer GENERATED ALWAYS AS IDENTITY NOT NULL,item varchar,total int,modified int); """db.execute(query)Manual SQL commandswithPreSQL(dbname=dbname,user=user,password=password,host=host,port=port)asdb:ifdb.connected():rows=db.execute("SELECT * FROM inventory LIMIT 5;").fetchall()forrowinrows:print(" -",row["item"])COUNT shortcutwithPreSQL(dbname=DATABASE,user=USER,password=PASSWORD,host=HOST,port=PORT)asdb:ifdb.connected():count=db.count("inventory",where="item='ramen'")print("Count:",count)SELECT shortcutwithPreSQL(dbname=DATABASE,user=USER,password=PASSWORD,host=HOST,port=PORT)asdb:ifdb.connected():rows=db.select("inventory","total",where="item='ramen'")forrowinrows:print(row["total"])INSERT ShortcutwithPreSQL(dbname=dbname,user=user,password=password,host=host,port=port)asdb:ifdb.connected():count=db.count("tokens",where="token='ethereum'")ifnotcount:db.insert("tokens",columns="token, usage, modified",values=f"('ethereum', 0,{modified})")Auto-Commit = False, RollbackwithPreSQL(dbname=DATABASE,user=USER,password=PASSWORD,host=HOST,port=PORT,autocommit=False)asdb:ifdb.connected():db.insert("inventory",columns="item, usage, modified",values=f"('jelly', 0,{modified})")# some failed transactionsdb.rollback()UPDATE shortcutwithPreSQL(dbname=DATABASE,user=USER,password=PASSWORD,host=HOST,port=PORT)asdb:ifdb.connected():total=0rows=db.select("inventory","total",where="item='ramen'")forrowinrows:total=int(row["total"])+1db.update("inventory",columns=f"total={total}",where="item='ramen'")print("Usage:",total)DELETE shortcutwithPreSQL(dbname=DATABASE,user=USER,password=PASSWORD,host=HOST,port=PORT)asdb:ifdb.connected():db.insert("inventory",columns="item, total, modified",values=f"('soy milk', 0,{modified})")count=db.count("inventory",where="item='soy milk'")print("Anon:",count)db.delete("inventory",where="item='soy milk'")count=db.count("inventory",where="item='soy milk'")print("Anon:",count)