Skip to main content
 首页 » 编程设计

python之将 django pyodbc 应用程序转换为 apache 时出现 odbc 连接错误

2024年08月28日36lvdongjie

我尝试了以下修复: https://code.google.com/p/pyodbc/issues/detail?id=214

我仍然遇到同样的错误。

关于如何补救这种情况有什么想法吗?

这是 odbc dns 列表的屏幕截图

Python代码

import pyodbc 
import datetime as dt 
from broker.models import Customer, Usage, Invoice, Document 
 
 
def import_customers(user_id,m_code): 
    cnxn = pyodbc.connect('DSN=as400;PWD=*********;DBQ=PRODDTA') 
    cursor = cnxn.cursor() 
 
    # the VNDRCODE of LQPPNATSLC is equivalent to the VENDORNUM of the LQPPNATVND !!! 
    # the VNDRCODE of LQPPNATVND is ACTUALLY the decriptive name of the broker 
    cursor.execute("""select b.CUSTNBR, 
                             b.CONAME, 
                             b.LASTNAME, 
                             b.FIRSTNAME, 
                             b.MIDDLEINT, 
                             b.STREET1, 
                             b.STREET2, 
                             b.CITY, 
                             b.STATE, 
                             b.ZIPCODE, 
                             b.ZIPEXTN, 
                             b.PHONENBR, 
                             a.PCID, 
                             c.VNDRCODE as VNAME, 
                             d.STREET1, 
                             d.CITY, 
                             d.STATE, 
                             d.ZIPCODE, 
                             a.VNDRCODE  
                    from LQPPNATSLC a, LQPPCUSMST b, LQPPNATVND c, LQPPDELDIR d 
                    where a.CUSTNBR = b.CUSTNBR and 
                          a.CUSTNBR = d.CUSTNBR and 
                          a.VNDRCODE = c.VENDORNUM and 
                          b.CUSTSTAT in ( '','RE') and 
                          a.VNDRCODE = ?  
                    order by b.CUSTNBR """,str(m_code)) 
 
    Customer.objects.filter(user_id=user_id).delete() 
    row = cursor.fetchone() 
    while row:       
        b = Customer( user_id = user_id, 
                    customer_number = row.CUSTNBR, 
                    company_name = row.CONAME, 
                    last_name = row.LASTNAME, 
                    first_name = row.FIRSTNAME, 
                    middle_initial = row.MIDDLEINT, 
                    street_address1 = row.STREET1, 
                    street_address2 = row.STREET2, 
                    city = row.CITY, 
                    state = row.STATE, 
                    zip = row.ZIPCODE, 
                    zip_ext = row.ZIPEXTN, 
                    phone = row.PHONENBR,                    
                    PCID = row.PCID, 
                    broker_name = row.VNAME, # see note above 
                    service_address1 = row.STREET1, 
                    service_city = row.CITY, 
                    service_state = row.STATE, 
                    service_zip = row.ZIPCODE, 
                    broker_code = row.VNDRCODE.strip() 
                    ) 
        b.save() 
        row = cursor.fetchone() 

来自 apache errror.log 的错误

[Mon Aug 26 13:50:41 2013] [error]   File "C:\\Python27\\Lib\\site-packages\\django\\core\\handlers\\base.py", line 115, in get_response 
[Mon Aug 26 13:50:41 2013] [error]     response = callback(request, *callback_args, **callback_kwargs) 
[Mon Aug 26 13:50:41 2013] [error]   File "C:\\Python27\\Lib\\site-packages\\django\\contrib\\auth\\decorators.py", line 25, in _wrapped_view 
[Mon Aug 26 13:50:41 2013] [error]     return view_func(request, *args, **kwargs) 
[Mon Aug 26 13:50:41 2013] [error]   File "C:\\dev\\broker_portal\\broker\\views.py", line 57, in customer_list 
[Mon Aug 26 13:50:41 2013] [error]     as400.import_customers(user_id,marketer_code) 
[Mon Aug 26 13:50:41 2013] [error]   File "C:\\dev\\broker_portal\\broker\\as400.py", line 7, in import_customers 
[Mon Aug 26 13:50:41 2013] [error]     cnxn = pyodbc.connect('DSN=as400;PWD=*********;DBQ=PRODDTA') 
[Mon Aug 26 13:50:41 2013] [error] Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') 

Django调试错误

Error at /broker/5/3/ 
('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') 
Request Method: GET 
Request URL:    http://djangodev.shipley.com/broker/5/3/ 
Django Version: 1.5.1 
Exception Type: Error 
Exception Value:     
('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') 
Exception Location: C:\dev\broker_portal\broker\as400.py in import_customers, line 7 
Python Executable:  C:\Apache2\bin\httpd.exe 
Python Version: 2.7.5 
Python Path:     
['C:\\dev\\broker_portal', 
 'C:\\Python27', 
 'C:\\Python27\\Lib\\site-packages', 
 'C:\\Python27\\Scripts', 
 'C:\\Apache2', 
 'C:\\Windows\\system32\\python27.zip', 
 'C:\\Python27\\Lib', 
 'C:\\Python27\\DLLs', 
 'C:\\Python27\\Lib\\lib-tk', 
 'C:\\Apache2\\bin'] 
Server time:    Mon, 26 Aug 2013 13:50:41 -0400 

元数据

wsgi.multiprocess    
False 
HTTP_COOKIE  
'csrftoken=ukCmTMgFUDtnSBvbOWYexafiTa4FPmmV; sessionid=qnh4u7n6mishbc4ka1oofxfd2fhtiraw' 
SERVER_PROTOCOL  
'HTTP/1.1' 
SERVER_SOFTWARE  
'Apache/2.2.25 (Win64) mod_wsgi/3.5-BRANCH Python/2.7.5' 
SCRIPT_NAME  
u'' 
mod_wsgi.enable_sendfile     
'0' 
mod_wsgi.handler_script  
'' 
SERVER_SIGNATURE     
'' 
REQUEST_METHOD   
'GET' 
PATH_INFO    
u'/broker/5/3/' 
SystemRoot   
'C:\\Windows' 
QUERY_STRING     
'' 
HTTP_USER_AGENT  
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36' 
HTTP_CONNECTION  
'keep-alive' 
HTTP_REFERER     
'http://djangodev.shipley.com/accounts/login/' 
SERVER_NAME  
'djangodev.shipley.com' 
REMOTE_ADDR  
'10.1.6.182' 
mod_wsgi.request_handler     
'wsgi-script' 
wsgi.url_scheme  
'http' 
PATH_TRANSLATED  
'C:\\dev\\broker_portal\\broker_portal\\wsgi.py\\broker\\5\\3\\' 
SERVER_PORT  
'80' 
mod_wsgi.version     
(3, 5) 
mod_wsgi.input_chunked   
'0' 
SERVER_ADDR  
'10.1.4.161' 
DOCUMENT_ROOT    
'C:/Apache2/htdocs' 
mod_wsgi.process_group   
'' 
COMSPEC  
'C:\\Windows\\system32\\cmd.exe' 
SCRIPT_FILENAME  
'C:/dev/broker_portal/broker_portal/wsgi.py' 
SERVER_ADMIN     
'admin@example.com' 
wsgi.input   
<mod_wsgi.Input object at 0x00000000044BD9F0> 
HTTP_HOST    
'djangodev.shipley.com' 
mod_wsgi.callable_object     
'application' 
wsgi.multithread     
True 
PATHEXT  
'.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC' 
HTTP_CACHE_CONTROL   
'max-age=0' 
REQUEST_URI  
'/broker/5/3/' 
HTTP_ACCEPT  
'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' 
WINDIR   
'C:\\Windows' 
wsgi.version     
(1, 0) 
GATEWAY_INTERFACE    
'CGI/1.1' 
wsgi.run_once    
False 
wsgi.errors  
<mod_wsgi.Log object at 0x00000000044924F0> 
REMOTE_PORT  
'50753' 
HTTP_ACCEPT_LANGUAGE     
'en-US,en;q=0.8' 
mod_wsgi.application_group   
'DjangoDev.shipley.com|' 
mod_wsgi.script_reloading    
'1' 
wsgi.file_wrapper    
'' 
CSRF_COOKIE  
u'ukCmTMgFUDtnSBvbOWYexafiTa4FPmmV' 
HTTP_ACCEPT_ENCODING     
'gzip,deflate,sdch' 

httpd.conf修改

AliasMatch ^/([^/]*\.css) /dev/broker_portal/broker/css/$1 
 
# Alias /media/ /path/to/mysite.com/media/ 
Alias /static/ /dev/broker_portal/broker/static/ 
 
<Directory  /dev/broker_portal/broker/static> 
Order deny,allow 
Allow from all 
</Directory> 
 
#<Directory /path/to/mysite.com/media> 
#Order deny,allow 
#Allow from all 
#</Directory> 
 
WSGIScriptAlias / /dev/broker_portal/broker_portal/wsgi.py 
WSGIPythonPath /dev/broker_portal 
 
<Directory /dev/broker_portal/broker_portal> 
<Files wsgi.py> 
Order deny,allow 
Allow from all 
</Files> 
</Directory> 
# 
# Dynamic Shared Object (DSO) Support 
# 
# To be able to use the functionality of a module which was built as a DSO you 
# have to place corresponding `LoadModule' lines at this location so the 
# directives contained in it are actually available _before_ they are used. 
# Statically compiled modules (those listed by `httpd -l') do not need 
# to be loaded here. 
# 
# Example: 
# LoadModule foo_module modules/mod_foo.so 
 
LoadModule wsgi_module modules/mod_wsgi.so 

请您参考如下方法:

另一种方法是嵌入连接字符串,而不是在代码之外维护 DSN。您需要使用连接字符串语法,但这里是开始的地方(我正在使用 pyodbc.connect 文档中定义的混合字符串和关键字):

cnxn = pyodbc.connect('system=<insert system name';dbq='PRODDTA', 
                      driver='{iSeries Access ODBC Driver}', 
                      uid='<insert user name>', pwd='<insert password>') 

我发现这种方法使不可避免的代码迁移变得更加容易。