Connecting to Azure SQL with Python -


i trying connect sql database hosted in windows azure through mysqldb python.

i keep getting error mysql_exceptions.operationalerror: (2001, 'bad connection string.')

this information works when connecting through .net (vb, c#) not having luck here.

for below used server's name azure .database.windows.net correct way go this?

here code:

#!/usr/bin/python import mysqldb  conn = mysqldb.connect(host="<servername>.database.windows.net", user="myusername", passwd="mypassword", db="db_name")  cursor = conn.cursor() 

i have tried using pyodbc freetds no luck.

@kyle moffat, os on? here how can use pyodbc on linux , windows: https://msdn.microsoft.com/en-us/library/mt763261(v=sql.1).aspx

windows:

linux:

  • open terminal install microsoft odbc driver 13 linux ubuntu 15.04 +

     sudo su    wget https://gallery.technet.microsoft.com/odbc-driver-13-for-ubuntu-b87369f0/file/154097/2/installodbc.sh    sh installodbc.sh   
  • for redhat 6,7

    sudo su wget https://gallery.technet.microsoft.com/odbc-driver-13-for-sql-8d067754/file/153653/4/install.sh  sh install.sh  
  • install pyodbc

    sudo -h pip install pyodbc 

once install odbc driver , pyodbc can use python sample connect azure sql db

import pyodbc  server = 'tcp:myserver.database.windows.net'  database = 'mydb'  username = 'myusername'  password = 'mypassword'  cnxn = pyodbc.connect('driver={odbc driver 13 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() 

if not able install odbc driver can try pymssql + freetds

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 

once follow these steps, can use following code sample connect: https://msdn.microsoft.com/en-us/library/mt715796(v=sql.1).aspx


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -