Connecting to MSSQL Server 2017 from Apache HTTP Server via python script

梦想的初衷 提交于 2019-12-24 19:28:49

问题


What should be my configuration to connect with Apache from the below configuration:

SERVER    :   DESKTOP-C6RS3DO
User Name :   (Default:blank)
Password  :   (Default:blank)
Connection:   DESKTOP-C6RS3DO\Vitriv-Desktop

Even server Authentication is set to "SQL Server and Windows Authentication mode"

cnxn = pypyodbc.connect(driver='{ODBC Driver 13 for SQL Server}', server='DESKTOP-C6RS3DO', database='demo2016', Trusted_Connection='Yes')

OR

cnxn = pypyodbc.connect(
    r'DRIVER={ODBC Driver 13 for SQL Server};'
    r'SERVER=DESKTOP-C6RS3DO;'
    r'DATABASE=demo2016;'
    #r'UID=Vitriv-Desktop;'
    #r'PWD='
    r'Trusted_Connection=yes;'
    )

errors.log

[Wed Dec 06 14:40:05.679511 2017] [cgi:error] [pid 5592:tid 1340] [client ::1:52581] AH01215: 

Traceback (most recent call last):\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
   File "C:/Apache24/htdocs/vitriv.py", line 37, in <module>\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
     r'DRIVER={ODBC Driver 13 for SQL Server};'\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
   File "C:/Users/Vitriv-Desktop/AppData/Local/Programs/Python/Python36-32/Lib/site-packages\\pypyodbc.py", line 2454, in __init__\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
     self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly)\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
   File "C:/Users/Vitriv-Desktop/AppData/Local/Programs/Python/Python36-32/Lib/site-packages\\pypyodbc.py", line 2507, in connect\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
     check_success(self, ret)\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
   File "C:/Users/Vitriv-Desktop/AppData/Local/Programs/Python/Python36-32/Lib/site-packages\\pypyodbc.py", line 1009, in check_success\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
     ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi)\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
   File "C:/Users/Vitriv-Desktop/AppData/Local/Programs/Python/Python36-32/Lib/site-packages\\pypyodbc.py", line 987, in ctrl_err\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
     raise DatabaseError(state,err_text)\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html
 pypyodbc.DatabaseError: ('28000', "[28000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'WORKGROUP\\\\DESKTOP-C6RS3DO$'.")\r: C:/Apache24/htdocs/vitriv.py, referer: http://localhost/signup_page.html

Connecting via python shell executes successfully but not through web browser. why?


回答1:


ANSWER

try:
    cnxn = pypyodbc.connect('Driver={ODBC Driver 13 for SQL Server};'
                        'Server=DESKTOP-C6RS3DO;'
                        'Database=demo2016;'
                        'uid=sa;pwd=sa')

    print('connected <br/>')
    cursor = cnxn.cursor()

except :
    print("could not connect")
    cursor.close()
    cnxn.close()


来源:https://stackoverflow.com/questions/47670856/connecting-to-mssql-server-2017-from-apache-http-server-via-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!