Package: toolsql
toolsqltoolsql makes it easy to build sql queries and execute those queriestoolsql's goals:provide identical interfaces to sqlite + postgresqlprovide nearly identical interfaces for synchronous +asyncusageminimize startup import timemaximize read / write performancetight integration with connectorx and polarsmake it easy to drop into raw SQL when neededuse basic python datatypes instead of custom objectsmypy typing withstrict=TrueContentsInstallationExample UsageReferenceInstallationpip install toolsqlrequires python 3.7 - 3.11Example UsageSpecify Database ConfigurationSpecify Table SchemaDDL StatementsDML StatementsTransactionsasyncFunctionalitySpecify Database Configuration# sqlitedb_config={'dbms':'sqlite','path':'/path/to/sqlite/file.db',}# postgresqldb_config={'dbms':'postgresql','database':'<database_name>','hostname':'<hostname>','username':'<username>','password':'<password>',}Specify Table Schematable={'name':'weather','columns':[{'name':'year','type':'Integer','primary':True},{'name':'month','type':'Integer','primary':True},{'name':'rainfall','type':'Float'},{'name':'temperature','type':'Float'},{'name':'country','type':'Float'},],}DDL Statementswithtoolsql.connect(db_config)asconn:# CREATEtoolsql.create_table(table=table,conn=conn,confirm=True,)# DROPtoolsql.drop_table(table=table,conn=conn,confirm=True,)DML Statementsrows=[(2020,1,8.2,90,'Turkey'),(2020,5,1.1,50,'Germany'),(2020,9,7.4,60,'UK'),(2021,1,5.2,72,'France'),(2021,5,2.1,56,'Argentina'),(2021,9,6.4,68,'Sweden'),(2022,1,1.2,70,'Indonesia'),(2022,5,4.1,56,'Vietnam'),(2022,9,9.4,60,'India'),]withtoolsql.connect(db_config)asconn:# INSERTtoolsql.insert(rows=rows,table=table,conn=conn,)# SELECTtoolsql.select(where_equals={'country':'Turkey'},table=table,conn=conn,)# UPDATEtoolsql.update(values={'country':'Türkiye'},where_equals={'country':'Turkey'},table=table,conn=conn,)# DELETEtoolsql.delete(where_equals={'country':'Türkiye'},table=table,conn=conn,)Transactionswithtoolsql.connect(db_config)asconn:withtoolsql.transaction(conn)# will commit if context exits error-free# ...asyncFunctionalityasyncwithtoolsql.async_connect(db_config)asconn:awaittoolsql.async_select(with_equals={'country':'Turkey'},table=table,conn=conn,)asyncwithtoolsql.async_transaction():awaittoolsql.async_insert(rows=rows,table=table,conn=conn,)awaittoolsql.async_update(values={'country':'Türkiye'},where_equals={'country':'Turkey'},table=table,conn=conn,)awaittoolsql.async_delete(where_equals={'country':'Türkiye'},table=table,conn=conn,)ReferenceSupported Executorssqlite3: sqlite sync readsaiosqlite: sqlite async readspsycopg: postgres sync / async readsconnectorx: simple read queriesSELECTinput argumentsdistinctwhere_equalswhere_gtwhere_gtewhere_ltwhere_ltewhere_likewhere_ilikewhere_inwhere_ororder_bylimitoffsetSELECToutput formats'tuple': each row is a tuple'dict': each row is a dict'cursor': query cursor'polars': polars dataframe of rows'pandas': pandas dataframe of rows'single_tuple': single row of output as a tuple'single_tuple_or_none': single row of output as a tuple'single_dict': single row of output as a dict'single_dict_or_none': single row of output as a dict'cell': single column of single row'cell_or_none': single column of single row'single_column': single column