Skip to main content

SQL Command Magic for IPython

Project description

SQL Command Magic for IPython

An IPython magic extension for executing SQL queries against Microsoft SQL Server using the sqlcmd utility.

Features

  • Execute SQL queries directly from IPython/Jupyter notebooks
  • Connect to Microsoft SQL Server using simple parameters or connection strings
  • Execute SQL in batches separated by GO statements
  • Execute external SQL files using EXECUTE_SQL_FILE
  • Substitute Python variables in SQL queries
  • Debug mode for troubleshooting

Installation

pip install ipython-sqlcmd

Requirements

  • Python 3.7+
  • IPython 7.0+
  • Pandas 1.0+
  • Microsoft SQL Server
  • sqlcmd utility installed and available in PATH

Usage

Load the extension

%load_ext sqlcmd

Set connection using simplified syntax

# Connect using just the database name (uses defaults for other parameters)
%sqlcmd AdventureWorks

# Connect with custom server, username and password
%sqlcmd AdventureWorks --server=myserver --username=myuser --password=mypassword

# Connect using password from environment variable
%sqlcmd AdventureWorks --password-env=MY_SQL_PASSWORD

# Connect with additional connection options
%sqlcmd AdventureWorks --driver="ODBC Driver 18 for SQL Server" --no-encrypt

Set connection using full connection string (advanced)

%sqlcmd 'mssql+sqlcmd:///?odbc_connect=SERVER=myserver;DATABASE=mydb;UID=myuser;PWD=mypassword'

Execute SQL

%%sqlcmd
SELECT TOP 10 *
FROM MyTable
WHERE Column1 = 'Value'

Execute SQL with variable substitution

value = "SomeValue"

%%sqlcmd
SELECT TOP 10 *
FROM MyTable
WHERE Column1 = '$value'

Debug mode

%%sqlcmd --debug
SELECT TOP 10 *
FROM MyTable

Execute SQL from file

%%sqlcmd
EXECUTE_SQL_FILE 'path/to/query.sql'

Connection Parameters

When using the simplified connection syntax, the following parameters are available:

Parameter Short Flag Default Value Description
--server -s localhost SQL Server instance
--username -u sa SQL Server username
--password -p None SQL Server password
--password-env -e SSMS_PASSWORD Environment variable containing password
--driver -d ODBC Driver 17 for SQL Server ODBC driver name
--encrypt True Encrypt connection (default)
--no-encrypt Do not encrypt connection
--trust-certificate True Trust server certificate (default)
--no-trust-certificate Do not trust server certificate
--encoding -c utf-8 Output and SQL database encoding (e.g., utf-8, latin-1, cp1252)
--show-execution-time -t False Print execution time of SQL queries

License

MIT

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

ipython_sqlcmd-0.1.3.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

ipython_sqlcmd-0.1.3-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file ipython_sqlcmd-0.1.3.tar.gz.

File metadata

  • Download URL: ipython_sqlcmd-0.1.3.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for ipython_sqlcmd-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e8d9bb528d02a250dc5847ea6f95e910eaf0acf8f0443dfe86ebf1f5c765a3f5
MD5 2b850e86a7e7655e8144ccf9c6873672
BLAKE2b-256 28752153da3bdd6926a0ff117716548f72585427064d993a06be98dbd405d407

See more details on using hashes here.

File details

Details for the file ipython_sqlcmd-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ipython_sqlcmd-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for ipython_sqlcmd-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 157a494d0e5bf95611379291a6014b451eb50a8a182619acdbdaa5cfd47b968e
MD5 40942c98739a3d146d9963b5cc2c67dc
BLAKE2b-256 3dee84481dfe13d2e691d3373080b1d9db2792b5da92dd6321c81f1787b54f9a

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