Skip to main content

p2sql Pandas and PYODBC To SQL provides a simpler way to manage communications between python and SQL using dependencies such as: - pandas - pyodc - and numpy

Project description

p2sql

p2sql (Pandas and PYODBC To SQL) provides a simpler way to manage communications between python and SQL using dependencies such as:

  • pandas
  • pyodc
  • and numpy

Getting Started

import p2sql

Functions

connection_string()

connection_string(
    Server: str, 
    Database = 'master', 
    Driver = '{SQL Server}', 
    IntegratedSecurity = True, 
    user=None, 
    passw=None
) 

conection_string() stores the conection information, the only parameters that are required are the Server, ODBC Driver

Note: It is recommended to supply the databasename, by default "master" will be used

conn_str_master = p2sql.connection_string('servername')

The value of the variable conn_str_master will be:

   Driver={SQL Server};  
   Server=servername;  
   Database=master;  
   Trusted_Connection=yes;  

You can create a different p2sql connection string by specifying the database or by setting intergrated security to false and setting the username and password parameters.

Example:

    conn_str_master = p2sql.connection_string (  
        'Server Name',  
        '{Enter Driver name}',  
        'Database Name',  
        False,   
        'UserName',  
        'Password'  
    )  

connection()

connection( conection_string() )

Use connection() to establish a connectin to server. Here we use the above created connection string as the connection string parameter.

Note: You could write a custom connection string manually in the p2sql.connection() function 

Example:

p2sql.connection('Driver={SQL Server}; Server=servername; Database=master; Trusted_Connection=true;') 

or

connection = p2sql.connection(conn_str_master)

executeCreateDB()

executeCreateDB( connection(), listOfDBNames, printQuery=False  )

Use executeCreateDB() to create database(s) by using the executeCreateDB function , passing the conneciton variable and a python list of databasename strings

p2sql.executeCreateDB(connection(),['TestDatabase','databaseTest2'],True)

getListOfDbs( )

getListOfDbs( connection() )

Use getListOfDbs() to get the list of database, by passing the connection function to getListOfDBs

listOfDbs = p2sql.getListOfDbs(connection)

getListOfDbs() will result to a list of tuples containing (databaseName, databaseID)

Example:

[('master', 1), ('tempdb', 2), ('model', 3), ('msdb', 4), ('TestDatabase', 5), ('databaseTest2', 6)]

createTblQuery()

createTblQuery( database:str, tablename:str, columns:list, dataTypes=False, schema='dbo' )

createTblQuery() requires a database name, a table name, and a list of lists,

Example: [['Column Name','Data Type']]

where index 0 is the name of the column and index 1 is the SQL data type

columnsWithDataTypes = [
    ['name', 'nvarchar(255) NOT NULL' ],
    ['age', 'int NULL' ],
    ['location', 'nvarchar(100) NULL']
    ]

A list of columns without data types will default create a table with data types of 'nvarchar(MAX) NULL'

Example:

columnsWithoutDataTypes = [
['name'],
['age'],
['location']
]

TestTableQuery = p2sql.createTblQuery('databaseTest2','TestTable',columnsWithDataTypes,True)

Contents Of TestTableQuery variable:

IF OBJECT_ID('TestTable') IS Null
CREATE TABLE [TestDatabase].[dbo].[TestTable](
    name nvarchar(255) NOT NULL, 
    age int NULL , 
    location NVARCHAR(100) NULL
)

executeQueries()

executeQueries( connection(), listOfQueries, printQuery=False )

executeQueries() executes a list of query strings such as insert,create,update,etc. This function does not return a value it loops through a list of supplied queries and executes the query using the module pyodbc connection

p2sql.executeQueries(connection,[TestTableQuery])

Any executable query can replace the variable [TestTableQuery]

Example:

insertQuery = ["""
    INSERT INTO [databaseTest2].[dbo].[TestTable2]  
    (   
    name,   
    age,   
    location  
    )   
    VALUES   
    (   
        'Daivd',   
        '45',   
        'Detroit'  
    );  
"""]
p2sql.executeQueries(connection,[insertQuery]) 

this will execute the custom created insert query

Another way to insert values into a table without writing the query is to use the putInTable() function

putInTable()

putInTable( 
    connection(),
    tablename: str, 
    listOfColumnNames: list, 
    listOfListValues: list ,
    Database="" , 
    schema='dbo', 
    printQuery = False 
    )

putInTable() will create and run an insert execute query to the specified table, This function does not return a value, however the printQuery can be set to True to view the query that is inserted

columnNamesList = ['name', 'age', 'location']
listOfListValues = [
    ['Daivd','45','Detroit'],
    ['Dora','45','Null']
    ]

p2sql.putInTable(connection,'TestTable2',columnNamesList, listOfListValues,'databaseTest2') 

getFromTable()

getFromTable( 
    connection(), t
    ablename: str,Database="", 
    printQuery = False, 
    listOfColumnNames = [ '' ] , 
    whereClause = [[]] , 
    schema='dbo' 
    )

getFromTable() will run a select query against the conneciotn and return a list of tuples from the specified table

Example:

[('Daivd', 45, 'Detroit'), ('Mike', 45, 'Null')]
p2sql.getFromTable(connection,'TestTable2','databaseTest2')

merge()

merge(
    values: list, *example: [[]] 
    columns:list, 
    targetTable: str, 
    matchOnSource : list, 
    matchOnTarget : list, 
    WHEN_MATCHED_THEN_UPDATE_target = True, 
    NOT_MATCHED_BY_SOURCE_DELETE = True, 
    NOT_MATCHED_BY_SOURCE_query = "", 
    printQuery = False  
    )

merge() creates a MERGE query, which combines the INSERT, UPDATE, and the DELETE operations altogether, Creates a tempTable from the supplied lists

columns = ['name', 'age', 'location']

values = [
    ['Big Bird', '32', 'Seasame Street'], 
    ['Dora', '5', 'The Void']
    ]

Note: you can also use pandas DataFrame by using tolist() on df values Example:

values = df.values.tolist()  
columns = df.columns.tolist()
mergeQuery =  merge(values,columns,'TestTable',['name'],['name'])

The merge() function will return a MERGE query

 with DF_SQL_TBL_8675 as ( select * from(
    VALUES ('Big Bird','32','Seasame Street'),('Dora','5','The Void')) tempTable ([name],[age],[location])
    )

    MERGE TestTable AS Target
    USING  DF_SQL_TBL_8675      AS Source
    ON  Source.[name] = Target.[name]

    -- For Inserts
    WHEN NOT MATCHED BY Target THEN
        INSERT ([name],[age],[location])
        VALUES ( Source.[name] , Source.[age] , Source.[location] )

    -- For Updates
    WHEN MATCHED THEN UPDATE SET
     Target.[name] = Source.[name] , Target.[age] = Source.[age] , Target.[location] = Source.[location]
    -- For Deletes
    WHEN NOT MATCHED BY Source THEN DELETE;

Pandas DataFrame Functions

The following functions will convert pandas DataFrames to SQL queries.

import pandas as pd

df_createTblQuery()

df_createTblQuery( database: str, tablename: str, dataframe: pandas.DataFrame ,printQuery: bool, schema='dbo' )

df_createTblQuery(), takes the column headers from a pandas DataFrame and creates a SQL create Table query.

Note: that this query will not insert data into the created table even if the DataFrame contains data for that you would need to use df_insertQuery(). All of the datatypes will be of type NVARCHAR(MAX)
d = {'name': ["Big Bird", "Dora"], 'age': ['32', '5'], 'location': ['Seasame Street', 'The Void']  }

df = pd.DataFrame(data=d)

dataframeCreateQuery = p2sql.df_createTblQuery('databaseTest2','df_TestTable',df)

p2sql.executeQueries(connection,[dataframeCreateQuery],True)

df_insertQuery()

df_insertQuery( 
    database: str, 
    tablename: str, 
    dataframe: pandas.DataFrame, 
    printQuery= False, 
    schema='dbo' 
    )

df_insertQuery() will itterate through pandas dataframe and insert data into a sql database table.

Note that the DataFrame column namnes need to match the database column names.

d = {'name': ["Big Bird", "Dora"], 'age': ['32', '5'], 'location': ['Seasame Street', 'The Void']  }

df = pd.DataFrame(data=d)

dataframeQuery = p2sql.df_insertQuery('databaseTest2','df_TestTable',df)

results of variable dataframeQuery:

INSERT INTO [databaseTest2].[dbo].[TestTable2](name , age , location  )VALUES('Big Bird','32','Seasame Street'); 
INSERT INTO [databaseTest2].[dbo].[TestTable2](name , age , location  )VALUES('Dora','5','The Void');
p2sql.executeQueries(connection,[dataframeQuery],True)

p2sql.getFromTable(connection,"df_TestTable","databaseTest2",True)

df_mergeQueryTables()

df_mergeQuery(  
    database: str, 
    targetTable: str, 
    sourceTable: str, 
    target_source_columns: list,  
    dataframe: pandas.DataFrame, 
    mergeDelete: bool, 
    deleteStatement: str, 
    schema='dbo'  
    )  

df_mergeQueryTables() creates a merge query, merges data from source table to target table. as long as the supplied dataframe matches the source and target table.

 WHEN target table ON target_source_columns  NOT MATCHED BY TARGET   
   then insert source table rows with insert query  
     
WHEN source table column MATCHED target  
    then update target table with update query  
      
WHEN NOT MATCHED BY SOURCE   
    THEN DELETE   
      
Unless mergeDelete is set to True,  
    then set deleteStatement to whatever query you want to run when not matched by source    
df_mergeQueryTables = p2sql.df_mergeQuery("databaseTest2","df_TestTable","TestTable2",['name'],df,True,'')

p2sql.executeQueries(connection,[df_mergeQuery])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

p2sql-0.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

p2sql-0.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file p2sql-0.0.1.tar.gz.

File metadata

  • Download URL: p2sql-0.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for p2sql-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4e89ec6a7eac4487e469cfd515e655ddcd44fad6003acb31057ce0f5fd6740cd
MD5 8c6ab0ecfa3b5d29d01db1c5193ff09b
BLAKE2b-256 be4f1443858dccd8fa7e324f8c282939f7fe79587224e7311f4392eebcf3c66f

See more details on using hashes here.

File details

Details for the file p2sql-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: p2sql-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for p2sql-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9721e2d0f07ff0ddbe824d79ac4ea5933df948c1feb1eefffd917a73303c137f
MD5 e814d780966e8b3ae84cd0bf4e8d904a
BLAKE2b-256 57d56260b56072af8a11c27c38518bfd913b614161b8f63d3fe3a758925386c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page