Skip to content

Instantly share code, notes, and snippets.

@si-software-lab
Forked from rduplain/README.md
Created April 3, 2024 18:33
Show Gist options
  • Save si-software-lab/3de08367ab18c8604c092442489566b8 to your computer and use it in GitHub Desktop.
Save si-software-lab/3de08367ab18c8604c092442489566b8 to your computer and use it in GitHub Desktop.

Revisions

  1. @rduplain rduplain revised this gist Oct 17, 2011. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    *Goal:* Connect to MSSQL using FreeTDS / ODBC in Python.

    Host: Ubuntu 11.10 x86_64

    Install:

    sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
    pip install pyodbc
    pip install pyodbc sqlalchemy

    In /etc/odbcinst.ini:

  2. @rduplain rduplain created this gist Oct 17, 2011.
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    *Goal:* Connect to MSSQL using FreeTDS / ODBC in Python.

    Install:

    sudo apt-get install freetds-dev freetds-bin unixodbc-dev tdsodbc
    pip install pyodbc

    In /etc/odbcinst.ini:

    [FreeTDS]
    Description=FreeTDS Driver
    Driver=/usr/lib/odbc/libtdsodbc.so
    Setup=/usr/lib/odbc/libtdsS.so
    11 changes: 11 additions & 0 deletions hello_pyodbc.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    "Proof connection at pyodbc level."
    # Test pyodbc connection. Result is 42.
    # Note parameters in connection string, <PARAMETER>.

    import pyodbc


    conn = pyodbc.connect('DRIVER=FreeTDS;SERVER=<IP_OR_HOSTNAME>;PORT=1433;DATABASE=<DATABASE_NAME>;UID=<USERNAME>;PWD=<PASSWORD>;TDS_Version=8.0;')
    cursor = conn.cursor()
    for row in cursor.execute('select 6 * 7 as [Result];'):
    print row.Result
    14 changes: 14 additions & 0 deletions hello_sqlalchemy.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    "Proof connection at SQLAlchemy level, on top of pyodbc."
    # Test SQLAlchemy connection. Result is 42.
    # Note parameters in connection string, <PARAMETER>.

    import urllib

    from sqlalchemy import create_engine


    engine = create_engine('mssql+pyodbc:///?odbc_connect=' +
    urllib.quote_plus('DRIVER=FreeTDS;SERVER=<IP_OR_HOSTNAME>;PORT=1433;DATABASE=<DATABASE_NAME>;UID=<USERNAME>;PWD=<PASSWORD>;TDS_Version=8.0;')
    )
    for row in engine.execute('select 6 * 7 as [Result];'):
    print row.Result