How to let a local program invoke and process a database based on a Django-developed web site

Source: Internet
Author: User


Django-is a Python-based web development architecture that has a completely separate database processing mechanism from SQL query statements that allows developers who do not understand SQL syntax to easily work with database content (including INSERT, delete, update, and so on).

The requirements for this document are:

When there is a need for a bulk update of the background data for a web app, the usual solution is:

1. Organize the files into a fully adaptable format for the database table and use Load (MySQL) or copy (Postgres) to import the

2. This requires a program to call the database interface when the file content needs to be preprocessed, and the edge processing side to deposit

based on solution 2, there are the following tricky questions:
1. Do not understand SQL language
2. Need to re-learn the interface usage of PSYCOPG2, MYSQLDB, etc.
3. Different databases need to know different interfaces, different syntax is
as follows:
#!/usr/local/bin/python
import psycopg2, Sys,re
#import mysqldb, Sys,re
Ifile=sys.argv[1]
Itype=sys.argv[2]
conn = Psycopg2.connect (database= "Django", User= "Django", password= "******", host= "localhost", port= "5432") # #Postgres接口, connect to database
#conn = mysqldb.connect (host= ' localhost ', user= ' Django ', passwd= ' ****** ', db= ' Django ', port=3306)
cur = conn.cursor () # #MYSQL接口
file=file (ifile, ' R ')
For line in
File.readlines ():
array=line.split ("\ t")
for I in Range (0,len (array)):
if Re.search (R ' ^\s*null\s*$ ', Array[i]):  
array[i]= ""
Cur.execute (' INSERT into ' disease_disease ' (ID, "Name", "Count", "Description") VALUES (%s,%s,%s,%s) ', (array[0 ],ARRAY[1],ARRAY[2],ARRAY[3] ) # #Postgres, submit SQL Insert query statement
#cur. Execute (' INSERT into Disease_disease (id,name,count,description) VALUES (%s,%s,%s,%s) ', (Array[0],array[1],array[2],array[3]) )   # #MYSQL
conn.commit (
)
cur.close (
)
conn.close (
)

Django Solution:

Directly invoke the Django module and its configuration file, directly using the Django syntax and working with the database

Specific as follows:

#! /usr/local/bin/python

Import SYS, RE

sys.path.append ("/home/zhuying/website") # # Settings directory, i.e. background code project directory

Os.environ.setdefault ("Django_settings_module", "website.settings") # #调用该Project的配置文件

From onekp.models Import * # # call ONEKP the app's database model

Family=family.objects.get (id=1)

Family. Name= "Unknown"

Family. description= "Unknown"

Family.save ()

Note: This program is a normal Python program, independent of the Django Web App daemon

Higher value:


The higher value of the solution does not appear in the single table and single record query and processing, but in the complex database model, different tables between the foreign key index linkage processing.



This article is from the "Blue Fin Dolphin" blog, please be sure to keep this source http://likunheng.blog.51cto.com/9527645/1589042

How to let a local program invoke and process a database based on a Django-developed web site

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.