【问题标题】:Connecting python 3.3 to microsoft sql server 2008将 python 3.3 连接到 microsoft sql server 2008
【发布时间】:2013-07-01 18:36:40
【问题描述】:

我是 python 新手。我在我的 Windows 机器上使用 Pydev IDE 和 Eclipse 进行 python 编程。我正在使用 python 3.3 vern 并想连接 MS Sql Server 2008。有人可以建议我应该如何连接 MS Sql Server 2008。

【问题讨论】:

    标签: python sql-server-2008 python-3.x pydev


    【解决方案1】:

    我将用一个 pypyodbc 示例来扩充 mata 的答案。

    import pypyodbc
    connection_string ='Driver={SQL Server Native Client 11.0};Server=<YOURSERVER>;Database=<YOURDATABASE>;Uid=<YOURUSER>;Pwd=<YOURPASSWORD>;'
    connection = pypyodbc.connect(connection_string)
    SQL = 'SELECT * FROM <YOURTABLE>'
    
    cur = connection.cursor()
    cur.execute(SQL)
    
    cur.close()
    connection.close()
    

    【讨论】:

      【解决方案2】:

      pyodbc支持python3,可以连接到任何有odbc驱动的数据库,包括sql server。

      还有一个纯 python 实现 pypyodbc 也应该支持 python3。

      adodbapi 也声称可以使用 python3。

      Here您可以找到一个包含更多选项的列表。

      【讨论】:

        【解决方案3】:
        import pyodbc
        server = 'SERVIDORNOMEOUIP'
        database = 'MEUBANCO'
        username = 'USERSQL'
        password = 'SENHASQL'
        
        #for SQL Server 2008
        driver='{SQL Server Native Client 10.0}'
        
        cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password + ';')
        
        cursor = cnxn.cursor()
        
        cursor.execute("SELECT nome,senha FROM [tabusuariosenha]")
        
        row = cursor.fetchone()
        print ("CAMPO1  |  CAMPO2 " )
        while row:
            print (str(row[0]) + " " + str(row[1]))
            row = cursor.fetchone()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多