在 Apache 服务器上加载 pyodbc 模块以运行简单查询 mssql 服务器数据库的配置应该是什么 [英] What should be my configuration to load pyodbc module on Apache Server to run simple queries mssql server database
问题描述
因为我是 Python 新手.
As I'm new to python.
我需要了解与 Apache HTTP Server 的简单数据库连接.
I need to know simple database connectivity with Apache HTTP Server.
只是我需要在 Apache HTTP Server 中运行以下代码.
Just I need to run the below code in Apache HTTP Server.
import pyodbc
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};"
"Server=DESKTOP-C6;"
"Database=demo2017;"
"Trusted_Connection=yes;")
cursor = cnxn.cursor()
cursor.execute('SELECT * FROM person')
for row in cursor:
print('row = %r' % (row,))
cursor.close();
cnxn.close();
我尝试在 python shell 中运行.它成功执行.
但是使用 Apache HTTP 服务器会导致 500 内部服务器错误.
I have tried to run in python shell. It executed successfully.
But with Apache HTTP Server results in 500 Internal Server Error.
也在 httpd.conf 文件中:
Also in httpd.conf file:
LoadModule pyodbc_module "c:/users/desktop/appdata/local/programs/python/python36-32/lib/site-packages/pyodbc.cp36-win32.pyd"
结果
httpd: Syntax error on line 571 of C:/Apache24/conf/httpd.conf: Can't
locate API module structure `pyodbc_module' in file
C:/Users/Desktop/AppData/Local/Programs/Python/Python36-32/Lib/site-packages/pyodbc.cp36-win32.pyd:
No error
那么是否有任何模块或代码应该导入/修改以与 Apache 一起运行?
So are there any modules or code that should be imported/modified to run with Apache?
推荐答案
您的堆栈中缺少几个层,如果您想利用 pyodbc
,这些非常值得学习和使用的想法:
You're missing several layers to your stack, which are really good ideas to learn and use if you want to leverage pyodbc
:
- Apache(已安装)mod_wsgi(WSGI 是一种描述Web 服务器如何与 Web 应用程序通信)
- 一个保护您免受非常糟糕的事情发生的框架
但是,如果您只想将其显示在网页上,则可以将其作为 CGI 程序进行.这是个坏主意,但又快又脏.
However, if all you want is for that to appear on a web page, you can do it as a CGI program. This is a bad idea, but is quick and dirty.
您需要在任何输出之前添加它(在 for row
之前):
You'll need to add this before any output (before for row
):
print("Content-Type: text/html;charset=utf-8")
print()
然后按照以下说明配置 Apache 以运行您的 Python 脚本:https://www.linux.com/blog/configuring-apache2-run-python-scripts
And then follow these instructions to configure Apache to run your Python script: https://www.linux.com/blog/configuring-apache2-run-python-scripts
这篇关于在 Apache 服务器上加载 pyodbc 模块以运行简单查询 mssql 服务器数据库的配置应该是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!