Add, delete, modify, query, and operate on Mysql and djangomysql Databases

Source: Internet
Author: User

Add, delete, modify, query, and operate on Mysql and djangomysql Databases

The following describes how to add, delete, modify, and query django:

1. view. py

#-*-Coding: UTF-8 -*-
From _ future _ import unicode_literals
From django. http import HttpResponse

From polls. models import Test
From django. shortcuts import render

# Create your views here.
# Solve garbled characters
Import sys
Reload (sys)
Sys. setdefaultencoding ('utf-8 ')
# Database Operations
Def testdb (request ):
Test1 = Test (name = 'wen Hongyu 2 ')
Test1.save ()
Return HttpResponse ("<p> data added successfully! </P> ")

# Querying databases
Def selectDB (request ):

# Obtain all data rows through all () of the model manager objects, which is equivalent to SELECT * FROM
List = Test. objects. all ()
Returnvalue = []
For v in list:
Returnvalue. append (v. name)
Print v. name

Print "++ getting a single object ++"
# Getting a single object
Response1 = Test. objects. filter (id = 1)
Print response1
For v1 in response1:
Returnvalue2 = "id:", v1.id, "Name:", v1.name
Print returnvalue2

Print "++ LIMIT that the returned data is equivalent to OFFSET 0 LIMIT 2 in SQL; ++"
Response2 = Test. objects. order_by ('name') [0: 2]
Returnvalue3 = {}
For v2 in response2:
Returnvalue3 [v2.id] = v2.name

Print returnvalue3.items ()
Print "++ output result:"
Return HttpResponse (returnvalue3.items ())

# You can use save () or update () to modify data ():
Def updateDB (request ):
# Modify the name field of id = 1 and save it, which is equivalent to UPDATE in SQL
Test1 = Test. objects. get (id = 1)
Test1.name = 'Google'
Test1.save ()

# Another Method
# Test. objects. filter (id = 1). update (name = 'Google ')
# Modifying all columns
# Test. objects. all (). update (name = 'Google ')

Return HttpResponse ("data updated successfully ")

Def deleteDB (request ):
# Delete data with id = 1
Test1 = Test. objects. get (id = 3)
Test1.delete ()
Return HttpResponse ("data deleted successfully ")

2. urls. py
"""pythondjango URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from BlogDjango import views
from polls import views as pollsviews, search, search2

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/+\d', views.hello),
url(r'^base/', views.base),
url(r'^testdb$', pollsviews.testdb),
url(r'^querydb$', pollsviews.selectDB),
url(r'^updateDB$', pollsviews.updateDB),
url(r'^deleteDB$', pollsviews.deleteDB),
]

3、models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Test(models.Model):

name = models.CharField(max_length=20)


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.