Django Database usage (sqlite3 and MySQL)

Source: Internet
Author: User
Tags mysql create mysql create database

First, sqlite3  use 1, import sqlite3  confirm whether the system installed 2, into the current project directory, cmd run python, into the command line mode 3, Import sqlite3,    sqlite3.connect (' {path\name.db} ')   #大括号内表示自定义, real case No curly braces 4, modify settings.py file     DATABASES = {     ' Default ': {          ' ENGINE ':  ' django.db.backends.sqlite3 ',         ' NAME ' :  {path\name.db} ',         ' USER ':  ',          ' PASSWORD ':  ',         ' HOST ':   ',         ' PORT ':  ',     }}5, enter the database file interface , Run python manage.py shell   from django.db import connection    cur = connection.cursor ()     If there is no error, the configuration is successful 6, modify the models.py file, configure your own table      Example:     from django.db import models    from django.contrib import  admin        # create your models here.     class blogpost (models. Model):     title = models. Charfield (max_length = 150)     body  = models. TextField ()     timestamp = models. Datetimefield ()     class meta:    ordering = ['- Timestamp ']7, create database content    python manage.py syncdb    Enter as prompted, Indicates a successful connection note: The above only represents the creation, connection and use of the database, and does not contain any other content. Second, MYSQL1, confirm the installation of MySQL database 2, in MySQL CREATE database   command mode can use    CREATE DATABASE {name}  Default charset=utf8;3, modify settings.py file     DATABASES = {          ' Default ': {             ' ENGINE ':  ' Django.db.backends.mysql ',              ' name ':  ' {name} ',              ' USER ':  ' {username} ',              ' PASSWORD ':  ' {PASSWORD} ',              ' HOST ': ' localhost ',  #ip               ' PORT ': ' 3306 ',         }    }4, Modify the models.py file, configure your own tables     for example:     from django.db import models     from django.contrib import admin         # create your models here.    class blogpost (models. Model):    title = models. Charfield (max_length = 150)     body  = models. TextField ()     timestamp = models. Datetimefield ()     class meta:    ordering = ['- Timestamp '] 5, CREATE database content    python manage.py syncdb    Follow the prompts to enter, indicating a successful connection





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.