2012-11-15 40 views
9

另一位開發人員和我正在另一臺服務器上使用舊式SQL服務器數據庫(SQLEXPRESS)設置django(v1.4.2)項目。到目前爲止,我們已經能夠使用django-pyodbc從linux和mac連接數據庫,並且使用django-mssql從運行windows 7的筆記本電腦連接到數據庫。我想在筆記本電腦上使用django-pyodbc來保持環境同步。Django-pydobc窗口上的SQL服務器連接問題

在筆記本電腦:

  • pyodbc(3.0.6)已安裝並在非的Django的.py腳本我可以連接並運行SQL語句
  • 下載Django的pyodbc通過下載的zip 1.4 ;我不確定我是否正確安裝:
    • 我解壓縮了該文件,並運行了頂層目錄中的setup.py文件;它把一個SQL_SERVER目錄中複製此SQL_SERVER目錄的/ lib目錄/ site-packages目錄
  • 到/ Django的/ DB /後端
  • 創建一個PYTHONPATH環境變量指向/ Django的/ DB /後端/ SQL_SERVER
    • 不知道它應該指向/ site-packages/sql_server來代替嗎?
  • 創建ODBC數據源(系統DSN)
    • 測試連接選項的作用
  • Editted settings.py中的數據庫條目是幾乎完全一樣的Linux版本(詳情如下)

所以,它不工作,我也得到了以下錯誤消息,並且不知道下一步該怎麼做:

('08001', '[08001] [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (17) (SQLDriverConnect); [01000] [Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (53); [01S00] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)') 

我設置Django的settings.py文件像這樣:

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sql_server.pyodbc', 
     'NAME': 'test', 
     'USER': 'test', 
     'PASSWORD': 'something_else', 
     'HOST': 'mssqlx', 
     'PORT': '12345', 
     'OPTIONS': { 
      'driver': 'SQL Server', 
     }, 
    }, 
} 

在Linux上,我的設置文件有DATABASES項,像這樣:

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sql_server.pyodbc', 
     'NAME': 'test', 
     'USER': 'test', 
     'PASSWORD': 'something_else', 
     'HOST': 'mssqlx',  # ODBC DSN defined in /etc/freetds.conf 
     'PORT': '12345',  # Probably unneeded. Set in mssqlx 
     'OPTIONS': { 
      'driver': 'SQL Server', # ODBC driver name in /etc/odbcinst.ini 
      'extra_params': "TDS_VERSION=7.0" # Probably unneeded. Set in mssqlx 
     } 
    }, 
} 

不知道,如果它將有助於解決這個問題,但使用Django的MSSQL(僅在Windows上運行),將(工作)項是:

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlserver_ado', 
     'NAME': 'test', 
     'USER': 'test', 
     'PASSWORD': 'something_else', 
     'HOST': '199.555.0.10',   # changed for this example 
     'PORT': '12345', 
     'OPTIONS': {'provider': 'SQLOLEDB'} 
    }, 
} 

不要知道其他信息可能會有所幫助。感謝您提供任何幫助或見解。

----驗屍---- 這裏是最後的工作:在設置對於數據庫

部分條目:

'default': { 
     'ENGINE' : 'django.db.backends.sql_server.pyodbc', 
     'NAME'  : 'test_db_name', 
     'USER'  : 'test_db_user_name', 
     'PASSWORD' : 'password', 
     # ODBC DSN defined in /etc/freetds.conf 
     'HOST'  : 'mssql_test', 
     # Ignored for Windows; Required for Linux 
     'OPTIONS' : { 
      # ODBC driver name in /etc/odbcinst.ini 
      'driver': 'SQL Server', 
      # NOTE: dsn option is added dynamically later, for Windows 
     } 
    }, 

# The ODBC DSN name specified above as DATABASES.default.HOST is ignored on 
# Windows, where it must be specified as DATABASES.default.OPTIONS.dsn instead. 
# However, we haven't found a way to make DATABASES.default.OPTIONS.dsn work in 
# Linux (and probably the same for Mac). It causes the error: 
# Data source name not found, and no default driver specified 
# Therefore we add it here, but only for Windows. 
# Note: The username and pwd in the windows dsn file is apparently NOT used 
#  (b/c server hosts both test and prod database in same MSSQL 
#  instance, both test and prod dsn files happen to work - they have the 
#  same ip address and port number, but different username/password's) 
# 
# On 64-bit Windows, with our current 32-bit version of pyodbc, the DSN 
# must be created via: 
# C:\Windows\SysWOW64\odbcad32.exe 
# instead of the regular "ODBC Data Sources" app in Control Panel, which 
# invokes: 
# C:\Windows\system32\odbcad32.exe 
# 
# os.name is... 
#  nt  for Hans' laptop (Windows 7) 
#  posix for the "Amazon Linux AMI" (CentOS) on AWS 
#  posix for Fred's Mac 
if os.name == 'nt':  # Windows 
    DATABASES['cf']['OPTIONS']['dsn'] = 'mssql_test' 
+4

如果你是「驗屍」會回答你的問題,要麼作爲答案添加它並接受它,要麼刪除問題(所以它不會像未答覆一樣)。 – meataxe

回答

1

嘗試使用https://github.com/michiya/django-pyodbc-azure。這應該適用於Linux和Windows。

然後定義你的數據庫設置,例如:

DATABASES = { 
    'default': { 
     'ENGINE': 'sql_server.pyodbc', 
     'NAME': 'dbname', 
     'HOST': 'dsn_entry', 
     'PORT': 'port', 
     'USER': '', 
     'PASSWORD': 'pass', 
     'OPTIONS': { 
      'driver': 'FreeTDS', 
      'dsn': 'dsn_entry', 
      'host_is_server': True 
     } 
    } 
} 

在Windows中OPTIONS'driver'條目應爲:

'driver': 'SQL Native Client', 

編輯:哎呀,沒能看到你已經解決了這個問題。留下我的答案作爲參考。