django--connecting to a SQL Server database

Source: Internet
Author: User

django--connecting to a SQL Server database * Please note that I am using python3.5 and Django2.0.4

SQL Server is a closed-source, Microsoft-sourced relational database that runs on Windows and Linux platforms. Because of its closed-source characteristics, so there are fewer companies to use, but it is more magical, my company and a few of my friends are in the company's business is mainly crawler, all with SQL Server as the main database in use.

First, packaging

If you're a veteran of Django, you should know that Django doesn't support SQL Server by default; If you're just starting to get into Django, you should know that Django supports four databases by default: PostgreSQL, MySQL, Oracle, SQLite. The above four databases do not need to do too much work directly, only need you to modify the project in the settings.py file in the database.

But with SQL Server, you need to import something yourself.

The package that I'm involved in is my own. All can be installed through "Pip install XXX" and do not need to be imported in a Django file. In particular, it is necessary to note that the django-pyodbc-azure is to be installed, without it directly on the collapse.

Second, the code

After you finish the previous step, you will only need to modify the settings.py file in your project.

1DATABASES = {2     'default': {3         'NAME':' Screen',4         'ENGINE':'Sql_server.pyodbc',5         'HOST':'127.0.0.1',6         'PORT':'1433',7         'USER':'User',8         'PASSWORD':'Password',9         'OPTIONS':{Ten             'Driver':'SQL Server Native Client 10.0', One         } A     } -}

Here are a few things to note:

1. ' ENGINE ' This key-value pair is a fixed notation (as if the PIP install Pyodbc to take effect after a long time, a bit unclear)

The default port number for 2.sql server is 1433, but some companies may change the port number, so it's best to check to see if the port is occupied by SQL Server.

3. In connection with the default database, many people did not write the ' OPTIONS ' this thing, I did not write in the beginning, the result is not even. After the internet to check, many people say in the ODBC database tuning of the engine (company Windows do development system, do not spray ...) )。 Results add in or not, and finally found that the ' OPTIONS ' did not write, after the completion of all the problems have been solved.

  This is also a long time ago to do things, just sit down and start to tidy up, so some things may be some omissions. If anyone saw this article, found some errors or missing parts please leave a message, I will promptly correct. Thank you! I hope this article can help the friends who need those help!

django--connecting to a SQL Server database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.