http://www.cnblogs.com/yijiaming/p/9684601.html
方法一:
1、需要安装pymssql
pip install pymssql
2、使用方法:
import pymssql conn=pymssql.connect(host='127.0.0.1',user='user',password='password',database='mydb') cur=conn.cursor() cur.execute('select * from table1')
#如果是插入、删除、更新语句切记要写提交命令con.commit() print (cur.fetchall()) cur.close() conn.close()
方法二:
1、安装必要的组件:
pip install django-sqlserver django-pytds pyodbc django-pyodbc pypiwin32
2、修改settings.py的databases:
databases = {
# ‘default’: {
# ‘engine’: ‘django.db.backends.sqlite3’,
# ‘name’: os.path.join(base_dir, ‘db.sqlite3’),
# }
‘default’: {
‘engine’: ‘sqlserver’,
‘name’: ‘mydb’,
‘host’: ‘127.0.0.1’,
‘port’: ‘1433’,
‘user’: ‘user’,
‘password’: ‘password,
‘options’: {
‘driver’: ‘sql server native client 10.0’,
},
}
}
方法三:
1、需要安装 sql server management studio 或者 manually install microsoft data access components (mdac)程序。
2、安装django-mssql和pywin32:
pip install django-mssql
3、修改settings.py的databases:
databases = { # 'default': { # 'engine': 'django.db.backends.sqlite3', # 'name': os.path.join(base_dir, 'db.sqlite3'), # } 'default': { 'name': 'mydb', 'engine': 'sqlserver_ado', 'host': '127.0.0.1', 'user': 'user', 'password': 'password', 'options': { 'provider': 'sqloledb', # have also tried 'sqlcli11' and 'sqloledb' 'extra_params': 'datatypecompatibility=80' }, } }
方法四(经过验证:django2.1.2和sql sever 2014 pycharm 2018.3)
步骤如下:
1、安装django-pyodbc-azure和pyodbc,
cmd命令:pip install django-pyodbc-azure pyodbc
2、修改settings.py的databases:
databases = {
'default': {
'engine': 'sql_server.pyodbc',
'name': 'productoutwarehouse',
'user': 'sa',
'password': '33661',
'host': '127.0.0.1',
'port': '',
'options': {
'driver':'sql server native client 11.0',
'mars_connection': true,
},
},
}
建立orm关系对象映射,数据迁移文件
python manage.py makemigrations
查看对应sql语句
python manage.py sqlmigrate
生成数据库数据表
python manage.py migrate
创建超级管理员
python manage.py createsuperuser