Package: psycopg2-wrappers
Psycopg2-WrapperPsycopg2-Wrapper is a Python library that provides a simple and easy-to-use interface for executing SQL queries using Psycopg2. It is designed to make it easy for developers to interact with PostgreSQL databases from Python applications.FeaturesSimple and easy-to-use interface for executing SQL queries using Psycopg2.Supports all standard SQL statements, including SELECT, INSERT, UPDATE, and DELETE.Provides methods for executing single and multiple queries, fetching results, and committing changes to the database.Built-in support for connection pooling.Lightweight and easy to install, with no external dependencies.InstallationTo install Psycopg2-Wrapper, you can use pip:pip install psycopg2-wrapperUsageTo use Psycopg2-Wrapper in your Python application, you first need to import one of the query executor classes from thepsycopg2_wrappermodule:frompsycopg2_wrapperimportSimpleQueryExecutor,NativeQueryExecutorConfigurationBefore you can execute SQL queries using Psycopg2-Wrapper, you need to configure the connection to the PostgreSQL server. You can do this by creating a configuration dictionary with the following fields:config={"host":"localhost","port":"5432",# if not specified, default port 5432 will be used"database":"mydatabase","user":"myusername","password":"mypassword"}host: The hostname of the PostgreSQL server.port: The port number of the PostgreSQL server.database: The name of the PostgreSQL database to connect to.user: The username to use for authentication.password: The password to use for authentication.Check out thePsycopg2 documentationfor more information about the configuration options.NativeQueryExecutorTheNativeQueryExecutorclass allows you to execute native SQL queries using Psycopg2. You can create an instance of the class and use itsexecute_querymethod to execute SQL queries:# create a NativeQueryExecutor instancequery_executor=NativeQueryExecutor(config)TheNativeQueryExecutorclass takes a configuration dictionary as describedhere.This class implements the following methods for executing SQL queries:Query to read data from the database:defexecute_and_fetchone(self,sql:str,params:tuple=None)->tuple:defexecute_and_fetchmany(self,sql:str,params:tuple=None,size:int=2)->list:defexecute_and_fetchall(self,sql:str,params:tuple=None)->list:And query to write/modify data to the database:defexecute_and_commit(self,sql:str,params:tuple=None)->None:defexecute_many_and_commit(self,sql:str,params:list)->None:Read data from the databaseExecute and fetchone# the sql queryquery_data_query="SELECT * FROM example_table WHERE id =%s"# the parameters of the queryparam=(1,)# execute the query and fetch the resultsresult=query_executor.execute_and_fetchone(query_data_query,param)Theexecute_and_fetchonemethod takes two parameters: theSQL queryto execute, andan optional tuple of parametersto pass to the query.
The method returns a tuple containing the results of the query.Execute and fetchmany# the sql queryquery_data_query="SELECT * FROM example_table WHERE id =%s"# the parameters of the queryparam=(1,)# execute the query and fetch the resultsresult=query_executor.execute_and_fetchmany(query_data_query,param,4)Theexecute_and_fetchmanymethod takes three parameters: theSQL queryto execute,an optional tuple of parametersto pass to the query, andan optional sizeparameter that specifies the maximum number of rows to fetch.Execute and fetchall# the sql queryquery_data_query="SELECT * FROM example_table WHERE id =%s"# the parameters of the queryparam=(1,)# execute the query and fetch the resultsresult=query_executor.execute_and_fetchall(query_data_query)Theexecute_and_fetchallmethod takes two parameters: theSQL queryto execute, andan optional tuple of parametersto pass to the query.Write/modify data to the databaseExecute and commit# the sql queryquery_data_query="INSERT INTO example_table (id, name) VALUES (%s,%s)"# the parameters of the queryparam=(1,'John')# execute the query and commit the changesquery_executor.execute_and_commit(query_data_query,param)Theexecute_and_commitmethod takes two parameters: theSQL queryto execute, andan optional tuple of parametersto pass to the query.Execute many and commit# the sql queryquery_data_query="INSERT INTO example_table (id, name) VALUES (%s,%s)"# the parameters of the queryparams=[(1,'John'),(2,'Jane'),(3,'Jack')]# execute the query and commit the changesquery_executor.execute_many_and_commit(query_data_query,params)Theexecute_many_and_commitmethod takes two parameters: theSQL queryto execute, anda list of tuples of parametersto pass to the query.Check out theNativeQueryExecutor examplefor more examples of how to use theNativeQueryExecutorclass.SimpleQueryExecutorTheSimpleQueryExecutorclass extends theNativeQueryExecutorclass and provides methods for executing simple SQL queries. Here are some usage examples:First we start by instantiating theSimpleQueryExecutorclass:# Define database configurationconfig={'host':'localhost','port':5432,'database':'my_database','user':'my_user','password':'my_password'}# Create an instance of SimpleQueryExecutorquery_executor=SimpleQueryExecutor(config)TheSimpleQueryExecutorclass takes a configuration dictionary as describedhere.Creating a table# Define the columns for the new tablecolumns={'id':'SERIAL PRIMARY KEY','name':'VARCHAR(255)','age':'INTEGER'}# Create the new tablequery_executor.create_table('my_table',columns)Thecreate_tablemethod takes two parameters: the name of the table to create, and a dictionary of column names and their data types.Selecting data from a table# Select all columns from the 'my_table' tableresults=query_executor.select_data('my_table')print(results)# Select only the 'name' and 'age' columns from the 'my_table' tableresults=query_executor.select_data('my_table',columns=['name','age'])print(results)# Select only the 'name' column from the 'my_table' table where age is greater than or equal to 18results=query_executor.select_data('my_table',columns=['name'],where='age >= 18')print(results)Theselect_datamethod takes three parameters: thename of the tableto select data from, alist of column names to select, andan optionalwhere_clauseparameterto filter the results.Inserting data into a table# Define the data to insertdata={'name':'John','age':25}# Insert the data into the 'my_table' tablequery_executor.insert_data('my_table',data)Theinsert_datamethod takes two parameters:the name of the tableto insert data into, anda dictionary of column names and their corresponding values.Dropping a table# Drop the 'my_table' tablequery_executor.drop_table('my_table')Thedrop_tablemethod takes one parameter:the name of the tableto drop.For more examples of how to use theSimpleQueryExecutorclass, check out theSimpleQueryExecutor example.