环境
deepin15.4
Python3.6.3
django1.11.6
一、安装MSSQL Server ODBC Driver
到微软的 Download Center 下载 MSSQL Server ODBC Driver
我下载的是最新的13.1版本的
搜索关键字odbc linux显示
点击download
点击download
选择linux版本下载
选择linux版本
卸载原有 unixODBC DriverManager
sudo apt-get remove unixodbc
这样做的好处是可以保证 configure 的各项参数符合要求,比如MSSQL的默认unicode字符集是UTF-16,所以 configure 参数里需要有 –with-iconv-ucode-enc=UTF16LE 。
依据下载的pdf中的命令安装odbc
我的deepin是15.4,相关资料按照Ubuntu 16.04的命令安装的
安装pyodbc和django-pyodbc
sudo pip install pyodbc django_pyodbc
二、配置odbc
打开odbcinst.ini 复制Driver
nano /etc/odbcinst.ini
复制红框内的内容
修改/etc/odbc.ini,
sudo nano /etc/odbc.ini
添加如下:
[ODBC Data Sources] 定义的mssql就是用于连接sql server,
settings中关键是需要定义dsn到上面配置好的odbc数据源
'dsn': 'MSSQL-PYTHON',
ODBCNAME = Microsoft SQL Server
[MSSQL-PYTHON]
Driver = /opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.4.0 #刚才复制的内容
Description = Hi! This is a description of the MSSQL-PYTHON driver
Trace = No
#Database = cxonline
Server = 192.168.0.47 #服务器ip
Port = 1433 #服务器端口
三、修改 django中的settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'icgoo_manage', # Or path to database file if using sqlite3.
'USER': 'lidongdev', # Not used with sqlite3.
'PASSWORD': 'xxxxx', # Not used with sqlite3.
'HOST': '192.168.0.8', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
},
'mssql': {
'NAME': 'cxonline_new',
'ENGINE': 'sql_server.pyodbc',
'HOST': '192.168.0.47',
'USER': 'sa',
'PASSWORD': 'xxxxxx',
'PORT': '1433', 相关资料 # Set to empty string for default. Not used with sqlite3.
'OPTIONS': {
'host_is_server': False,
'dsn': 'MSSQL-PYTHON',
},
}
}
DATABASE_OPTIONS = {
'host_is_server': False,
'dsn': 'MSSQL-PYTHON',
}
定义的mssql就是用于连接sql server,
settings中关键是需要定义dsn到上面配置好的odbc数据源
‘dsn’: ‘MSSQL-PYTHON’,
相关资料
https://www.tuicool.com/articles/A3UjAr3
http://xiaolin0199.iteye.com/blog/2022562
http://www.cnblogs.com/jasonli-01/p/6689177.html