Connecting to Azure SQL with Python
<p>I am trying to connect to a SQL Database hosted in Windows Azure through MySQLdb with Python. </p>

<p>I keep getting an error mysql_exceptions.OperationalError: (2001, 'Bad connection string.')</p>

<p>This information works when connecting through .NET (vb, C#) but I am definitely not having any luck here. </p>

<p>For  below I used my server's name from azure then .database.windows.net   Is this the correct way to go about this?</p>

<p>Here is my code:</p>

<pre><code>#!/usr/bin/python
import MySQLdb

conn = MySQLdb.connect(host="&lt;servername&gt;.database.windows.net", user="myUsername", passwd="myPassword", db="db_name")

cursor = conn.cursor()
</code></pre>

<p>I have also tried using pyodbc with FreeTDS with no luck.</p>

<p>@Kyle Moffat, what OS are you on? Here is how you can use pyodbc on Linux and Windows:
<a href="https://msdn.microsoft.com/en-us/library/mt763261(v=sql.1).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/mt763261(v=sql.1).aspx</a></p>

<p><strong>Windows:</strong></p>

<ul>
<li>Download and install Python</li>
<li><p>Install the Microsoft ODBC Driver 11 or 13: </p>

<ul>
<li>v13: <a href="https://www.microsoft.com/en-us/download/details.aspx?id=50420" rel="nofollow">https://www.microsoft.com/en-us/download/details.aspx?id=50420</a> </li>
<li>v11: <a href="https://www.microsoft.com/en-us/download/details.aspx?id=36434" rel="nofollow">https://www.microsoft.com/en-us/download/details.aspx?id=36434</a></li>
</ul></li>
<li><p>Open cmd.exe as an administrator</p></li>
<li><p>Install pyodbc using pip - Python package manager</p>

<pre><code>cd C:\Python27\Scripts&gt;  
pip install pyodbc
</code></pre></li>
</ul>

<p><strong>Linux:</strong></p>

<ul>
<li><p>Open terminal
Install Microsoft ODBC Driver 13 for Linux For Ubuntu 15.04 +</p>

<pre><code> sudo su  
 wgetâ¯https://gallery.technet.microsoft.com/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.shâ¯ 
 sh installodbc.sh  
</code></pre></li>
<li><p>For RedHat 6,7</p>

<pre><code>sudo su
wgetâ¯https://gallery.technet.microsoft.com/ODBC-Driver-13-for-SQL-8d067754/file/153653/4/install.sh 
sh install.sh 
</code></pre></li>
<li><p>Install pyodbc</p>

<pre><code>sudo -H pip install pyodbc
</code></pre></li>
</ul>

<p>Once you install the ODBC driver and pyodbc you can use this Python sample to connect to Azure SQL DB</p>

<pre><code>import pyodbc 
server = 'tcp:myserver.database.windows.net' 
database = 'mydb' 
username = 'myusername' 
password = 'mypassword' 
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT @@version;") 
row = cursor.fetchone() 
while row: 
    print row[0] 
    row = cursor.fetchone()
</code></pre>

<p>If you are not able to install the ODBC Driver you can also try pymssql + FreeTDS</p>

<pre><code>sudo apt-get install python
sudo apt-get --assume-yes install freetds-dev freetds-bin
sudo apt-get --assume-yes install python-dev python-pip
sudo pip install pymssql==2.1.1
</code></pre>

<p>Once you follow these steps, you can use the following code sample to connect:
<a href="https://msdn.microsoft.com/en-us/library/mt715796(v=sql.1).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/mt715796(v=sql.1).aspx</a> </p>

python azure sql-azure