a module that makes queries easier to SQL Server than PYODBC
Project description
SQLSERVER PYODBC Module
Installation for linux guys
- Install a driver for your linux machine!
- I recommend
FreeTDS
Installing FreeTDS
- Install pre-requisite packages
sudo apt-get install unixodbc unixodbc-dev freetds-dev freetds-bin tdsodbc
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sqlserver-0.0.16.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15ad28dc945ab88f8cdf4a16a74787a6bc5daa662ff4ce96bfb2a380c02ca7c7 |
|
MD5 | 785aa7b6a12bfe3d74a4c50399344c1a |
|
BLAKE2b-256 | ebf4a9aad8ed157927ae97857b31712a0c9fdc39195ed136c0f776b50ba216b6 |