Skip to main content

a module that makes queries easier to SQL Server than PYODBC

Project description

SQLSERVER PYODBC Module

Installation for linux guys

  1. Install a driver for your linux machine!
  2. I recommend FreeTDS

Installing FreeTDS

  1. Install pre-requisite packages
sudo apt-get install unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
  1. Point odbcinst.ini to the driver in /etc/odbcinst.ini
[FreeTDS]
Description = v0.91 with protocol v7.2
Driver = MYDRIVERPATH

where MYDRIVERPATH is the path of the libtdsodbc.so file

Hint! Look in the /usr/lib/mylinuxdistro/odbc folder! Will implement script in the future to install/automate this for linux solutions.

pip install sqlserver

Installation for windows guys

pip install sqlserver

Example of ConnectionString

DRIVERS = db.ReturnDrivers()
# output of drivers
['SQL Server', 'ODBC Driver 17 for SQL Server', 'SQL Server Native Client 11.0', 'SQL Server Native Client RDA 11.0', 'Microsoft Access Driver (*.mdb, *.accdb)', 'Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)', 'Microsoft Access Text Driver (*.txt, *.csv)']

# We can use a SQL ODBC Driver or FreeTDS
DRIVER={ODBC Driver 17 for SQL Server};SERVER=SERVERNAME,PORT;DATABASE=DB;UID=USERNAME;PWD=PASSWORD

Usage

Initialization

import sqlserver
db = sqlserver.adgsqlserver('yourconnectionstring')

Commands

Execute Query

parms: ExecuteQuery(query:str)

This enables you to execute any query without any stdout but returns a bool True or False if query passes and logs exception in terminal as stdout.

query = 'somequery'
db.ExecuteQuery()

Return Query as Dictionary

parms: GetRecordsAsDict(query:str)

We use this for select statements or any other query that returns a table as a result.

query = "SELECT 'Connection Passed' AS Result"
db.GetRecordsAsDict(query)

stdout

{'results': [{'Result': 'Connection Passed'}]}

Return Query as Column List

parms: GetRecordsOfColumn(query:str,ColumnName:str)

We use this for select statements or any other query that returns a table as a result.

db.GetRecordsOfColumn("SELECT 'Connection Passed' AS Result", "Result")

stdout

['Connection Passed']

Create CSV Table Schema

parms: CreateCSVTable(csvfile:str)

Creates a SQL Table with varchar(max) columns such that it can be ready to be inserted to based on the .csv column names

Assumption: somefile.csv

name,surname,phonenumber
test,testor,01234567810
path = 'C:/somefile.csv'
db.CreateCSVTable(path)

In SQL Table somefile.dbo

|name|surname|phonenumber|

Insert CSV Table

parms: InsertCSVTable(csvfile:str)

Assumption: somefile.csv

name,surname,phonenumber
test,testor,01234567810
path = 'C:/somefile.csv'
db.InsertCSVTable(path)

In SQL Table somefile.dbo

--------------------------
|name|surname|phonenumber|
|----|-------|-----------|
|test|testor |01234567810|
--------------------------

Insert XML

parms: InsertXMLSQLTable(xmlfilepath:str)

xmlfilepath = 'C:/somexml.xml'
db.InsertXMLSQLTable(xmlfilepath)
Copyright ADGSTUDIOS 2022

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

sqlserver-0.0.16.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

sqlserver-0.0.16-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file sqlserver-0.0.16.tar.gz.

File metadata

  • Download URL: sqlserver-0.0.16.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.14

File hashes

Hashes for sqlserver-0.0.16.tar.gz
Algorithm Hash digest
SHA256 775724d29c890015a3e7e517507afeab1afd67975ea224b37ea3ec875f70d82b
MD5 480e1f54469929506fb58980c9736b4a
BLAKE2b-256 27e2795a19a9aed9712d7b0656752e4107efac5b252358d57bda5bf66ec9ea6e

See more details on using hashes here.

File details

Details for the file sqlserver-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: sqlserver-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.14

File hashes

Hashes for sqlserver-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 29b1beb70eb14d9c570bd866c3b73264700bef57697eca1140c9e8b74ac1c29e
MD5 d0df4e531a67c791882190c1a08c148a
BLAKE2b-256 fa52daae82c139ef1fb5875b4fd98da043da02fdd0c310d31ee0fd65240b17fa

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