使用Python pypyodbc连接到SQL服务器的字符串

时间:2017-06-13 11:27:29

标签: python sql-server database-connection pypyodbc

我正在使用此连接字符串,但有些错误。

conn = pyodbc.connect
(DRIVER={SQL Server};SERVER=localhost;DATABASE=test;UID=YYY;PWD=XXXX)
connection.close()

我该怎么做?

5 个答案:

答案 0 :(得分:2)

如果您使用的是Linux或macOS,则需要安装MS ODBC驱动程序(可在其网站上找到) https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017

然后使用驱动程序字符串{ODBC Driver 17 for SQL Server}

connection_string = "DRIVER={ODBC Driver 17 for SQL Server};Server=..."

答案 1 :(得分:1)

假设您使用的是MacOS,请首先使用Homebrew安装msodbcsql:

brew tap microsoft/msodbcsql https://github.com/Microsoft/homebrew-msodbcsql
brew install msodbcsql

然后用Python:

如果您尚未安装pypyodbc,请使用pip进行安装:

pip install pypyodbc

然后创建一个Python脚本以连接到数据库:

import pypyodbc as pyodbc

cnxn = pyodbc.connect("DRIVER={ODBC Driver 13 for SQL Server};"
                  "SERVER=localhost;"
                  "DATABASE=test;"
                  "UID=YYY;"
                  "PWD=XXX;"
                  "TrustServerCertificate=no;"
                  "Connection Timeout=60")

答案 2 :(得分:0)

试试这个

import pypyodbc

connection = pypyodbc.connect(" DRIVER = {SQL Server}; Server = servername; DATABASE = MyDatabase; Trusted_Connection = Yes")

答案 3 :(得分:0)

jQuery(document).ready(function($) { $('input#ppm').keyup(function(){ if( $('#ppm').val() != '') { $('#mgm3').attr('disabled', true); calculateMGM3(); } else { $('#mgm3').attr('disabled', false); } }); $('input#mgm3').keyup(function(){ if( $('#mgm3').val() != '') { $('#ppm').attr('disabled', true); calculatePPM(); } else { $('#ppm').attr('disabled', false); } }); $('input#mol').keyup(function(){ if( $('#mgm3').prop('disabled') ) { calculateMGM3(); } if( $('#ppm').prop('disabled') ) { calculatePPM(); } }); function calculateMGM3() { var mol = parseFloat($('#mol').val()); var ppm = parseFloat($('#ppm').val()); var calcMGM3 = parseFloat(( ppm * mol ) / 24.45 ).toFixed(2); $('#mgm3').val( calcMGM3 ); } function calculatePPM() { var mol = parseFloat($('#mol').val()); var mgm3 = parseFloat($('#mgm3').val()); var calcPPM = parseFloat(( mgm3 * 24.45 ) / mol ).toFixed(2); $('#ppm').val( calcPPM ); } });

请参阅https://www.connectionstrings.com/sql-server/

答案 4 :(得分:0)

import pypyodbc
connection = pypyodbc.connect('Driver={SQL Server};'
  'Server=localhost;'
  'Database=test;'
  'uid=YYY;pwd=XXX')