使用Python连接到Azure SQL

编程入门 行业动态 更新时间:2024-10-12 03:18:08
本文介绍了使用Python连接到Azure SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用Python通过MySQLdb连接到Windows Azure中托管的SQL数据库.

I am trying to connect to a SQL Database hosted in Windows Azure through MySQLdb with Python.

我一直收到错误mysql_exceptions.OperationalError:(2001,'Bad connection string.')

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

通过.NET(vb,C#)连接时,此信息有效,但是我在这里绝对没有运气.

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

在下面,我先使用azure的服务器名称,然后使用.database.windows,这是解决此问题的正确方法吗?

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

这是我的代码:

#!/usr/bin/python import MySQLdb conn = MySQLdb.connect(host="<servername>.database.windows", user="myUsername", passwd="myPassword", db="db_name") cursor = conn.cursor()

我也尝试将pyodbc与FreeTDS结合使用,但没有运气.

I have also tried using pyodbc with FreeTDS with no luck.

推荐答案

@Kyle Moffat,您使用的是什么操作系统?这是在Linux和Windows上如何使用pyodbc的方法: msdn.microsoft/zh-我们/library/mt763261(v=sql.1).aspx

@Kyle Moffat, what OS are you on? Here is how you can use pyodbc on Linux and Windows: msdn.microsoft/en-us/library/mt763261(v=sql.1).aspx

Windows:

  • 下载并安装Python
  • 安装Microsoft ODBC驱动程序11或13:

  • Download and install Python
  • Install the Microsoft ODBC Driver 11 or 13:

  • v13: www.microsoft/zh-cn/download/details.aspx?id = 50420
  • v11: www.microsoft/zh-cn/download/details.aspx?id = 36434
  • v13: www.microsoft/en-us/download/details.aspx?id=50420
  • v11: www.microsoft/en-us/download/details.aspx?id=36434

以管理员身份打开cmd.exe

Open cmd.exe as an administrator

使用pip安装pyodbc-Python程序包管理器

Install pyodbc using pip - Python package manager

cd C:\Python27\Scripts> pip install pyodbc

Linux:

  • 打开终端 为Ubuntu 15.04安装适用于Linux的Microsoft ODBC驱动程序13 +

  • Open terminal Install Microsoft ODBC Driver 13 for Linux For Ubuntu 15.04 +

sudo su wget gallery.technet.microsoft/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.sh  sh installodbc.sh

  • 对于RedHat 6,7

  • For RedHat 6,7

    sudo su wget gallery.technet.microsoft/ODBC-Driver-13-for-SQL-8d067754/file/153653/4/install.sh sh install.sh

  • 安装pyodbc

  • Install pyodbc

    sudo -H pip install pyodbc

  • 一旦安装了ODBC驱动程序和pyodbc,就可以使用此Python示例连接到Azure SQL DB

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

    import pyodbc server = 'tcp:myserver.database.windows' 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()

    如果您无法安装ODBC驱动程序,也可以尝试pymssql + FreeTDS

    If you are not able to install the ODBC Driver you can also 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

    按照这些步骤进行操作之后,就可以使用以下代码示例进行连接: msdn.microsoft/zh-我们/library/mt715796(v=sql.1).aspx

    Once you follow these steps, you can use the following code sample to connect: msdn.microsoft/en-us/library/mt715796(v=sql.1).aspx

    更多推荐

    使用Python连接到Azure SQL

    本文发布于:2023-11-23 13:04:15,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1621546.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:连接到   Python   SQL   Azure

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!