Skip to main content

Magical SQL Server

A tool that integrates Microsoft SQL Server into applications like magic.

What is this?

Do you have an application that needs to consume data stored on SQL Server Database, or needs to post data into this database? Magical SQL Server will handle that for you easily.

Requirements

To use this module, you have to install python3 and pymssql:

pip3 install pymssql

Bulk insert method uses BCP to copy multiple rows efficiently. See Microsoft Documentation to install it on your machine.

Usage

Let's say that you have a table called my_database.dbo.users:

id name team
1 example example
2 new_user users

To retrieve all the data from the table:

from magical_sqlserver import SQLServer
sql = SQLServer (
    user,
    password,
    host,
    my_database,
    port=1433,
)

data = sql.select("users")

This will return:

[
  {
    "id": 1,
    "name": "example",
    "team": "example"
  },
  {
    "id": 2,
    "name": "new_user",
    "team": "users"
  }
]

You can filter columns and get specific columns:

data = sql.select("users", condition={"id":1}, columns=["name"])

With result {"name":"example"}.

With Context

You may use with statement then Magical SQL Server will open and close a connection for you as your statement ends:

with SQLServer(user, host, password, my_database) as sql:
    sql.select("users")

You can close your connection manually with sql.close() method too.

As a decorator

You may want to decorate your funciton with this module. To to that, simply use the provide_session module. It will try replace sql argument if it exists in your funcion, or add a kwarg argument called sql. This decorator opens an close an SQL Server connection for you. Example:

from magical_sqlserver import provide_session

@provide_session(user, host, password, my_database)
def awesome_function(sql=None):
  sql.select("users")

Or with kwargs:

@provide_session(user, host, password, my_database)
def awesome_function(**kwargs):
  sql = kwargs["sql"]
  sql.select("users")

Writing data

Single records

To create new record:

data = {"id":3, "name":"bar"}

sql.insert("users", data)

To update existing records:

sql.update("users", data={"name":"new_name"}, contition={"id":1})

You can delete records too:

sql.delete("users", {"id":2, "name":"new_user"})

It's important to say that all conditions are additive. For example, if your update has condition {"id": 1, "name": "new_user"}, this module will build an sql query that has id = 1 and name = 'new_user' and will try this against the database. In the table above, no data will be updated.

Bulk Insert

Simply pass a list of dicts to bulk_insert method. It will transform it to csv temporary files and copy it to table with BCP method. See requirements for more details.

data = [{"id":4, "name":"me"},{"id":5, "name":"you"}]

sql.bulk_insert("users", data)

Other Stuff

You can run generic queries with the method query:

data = sql.query("select top(10) * from my_table join users on my_table.id = users.id")

And retrieve all tables from Sql Server Schema:

tables_list = sql.tables()

Metadata

Release files for magical-sqlserver 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for magical-sqlserver 0.4.0
File Size Uploaded Provenance Actions
magical_sqlserver-0.4.0.tar.gz 4.8 kB Without provenance Details for magical_sqlserver-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for magical-sqlserver 0.4.0
File Interpreter ABI Platform Actions
magical_sqlserver-0.4.0-py3-none-any.whl Python 3 none any Details for magical_sqlserver-0.4.0-py3-none-any.whl

Total release size: 11.0 kB

Release files / magical_sqlserver-0.4.0.tar.gz

Download URL magical_sqlserver-0.4.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
96b98762109200b3eedd1e102eac8e24463926f3c436c711677703a5f9e9af93
BLAKE2b-256 checksum
How to use checksums
c2c85717f67515953b7d29ec492530b4c1b37b81ec1335f5b09fb5bc8b8f48bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

Release files / magical_sqlserver-0.4.0-py3-none-any.whl

Download URL magical_sqlserver-0.4.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4eff87b064ebd5fa4099045a4c37d3cfc6ed404efc05e270c262846ea131d52
BLAKE2b-256 checksum
How to use checksums
ce0259b57d74da76ec48b4c1636fb64bec239c245da1ae699e4ac582c873ad1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.18.4 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

Security for magical-sqlserver 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page