Database connection wrapper
<p>I have written the following class to make life easier for me:</p>

<pre><code>import pymssql

class DatabaseConnection:
    def __init__(self):
        self.connection = pymssql.connect(host='...', user='...', password='...', database='...', as_dict=True)

    def select(self, statement, arguments={}):
        cur = self.connection.cursor()
        cur.execute(statement, arguments)
        return list(cur)

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        if self.connection:
            self.connection.close()   
</code></pre>

<p>I use it like this:</p>

<pre><code>&lt;script language = "Python" runat="server"&gt;
    # get all that data we need to present on this asp page
    with database.DatabaseConnection() as connection:
        orders = connection.select('select * from ordersview')
        special_orders = connection.select('select * from ordersview where paymenttype = %(paymentType)s', {'paymentType': 'Card'})
&lt;/script&gt;

&lt;script language = "Python" runat="server"&gt;
    # later on lets use that data
    for row in special_orders:
        ...
&lt;/script&gt;
</code></pre>

<p>I intend on having a connection host class later to manage the database hosts I wish to connect to but for now I hard code this.</p>

<p>Have I done anything here which is not recommended or unsafe in your opinion? Is it a reasonable design? Is it ok to return list(cur) since the iterator will be out of scope otherwise, outside the with scope?</p>

<p>Thanks for your help,</p>

<p>Barry</p>
