Data cleansing in the database description: In the system I use, due to history and various reasons, the data recorded in the table in the database is problematic, there are duplicate and incomplete data solutions: First of all, because the amount of these data is still very large, manual cleanup must not, and then, I just want to write a SQL script to update according to the rules of the Convention, you can use the cursor to complete the traversal of the records in the table, but SQL is oriented to the Structured query language, not process-oriented, so although can but not C and Python such a process-oriented easy to use, and later, I want to create a new method directly in my project and then call it through the address bar of the browser.
PS: Although the Django Orm is very convenient, but it is very embarrassing to use, some filter conditions and grammar rules, I still have to find examples on the Internet to know how to use fortunately to provide direct execution of the SQL statement method, I am in the process of cleaning, using the execution of native SQL statements.
The code is as follows:
Def datetimestr (): Return DateTime.Now (). Strftime ('%y%m%d-%h%m%s ') + ' >>> ' def update_dcdata (req): l Og_path= ' Apps/dc/l10n_reports ' Update_dcdata_log=open (log_path+ ' updateDcdataLog.log ', ' w+ ') sql_getprojectids= ' Select a.project_id from "(SELECT COUNT (*) num,project_id from dc_data where lablename=%s Group B Y project_id) A, "(SELECT COUNT (*) num, project_id from Management_project_target_lang Group by P roject_id) b ' where a.project_id=b.project_id and a.num!=b.num order by project_id ' Sql_ge Tallprojectids= ' Select project_id from Management_project ' sql_getlanguagesections= ' select B.name from management_p Roject_target_lang a,management_l10nlanguage B ' where a.project_id=%s and a.l10nlanguage_id= b.ID and b.id!= "1" ' sql_getrecords= ' select ID, languagesection,value from dc_data where lab Lename =%s and project_id=%s and important= "1" ' sql_addrecordbyid= ' insert into Dc_data (lablename,languagesection,type,value,project_id,task_id, ' ' Important,unit,settlement,workload) ' Select Lablename,languagesection,type,value,proje Ct_id,task_id,important,unit,settlement,workload ' from Dc_data where id=%s ' sql_updatelgs= ' update dc _data set languagesection=%s where id=%s ' sql_getlablenames= ' select Lablename from Dc_data where lablename like "percent _all "GROUP by Lablename ' Update_dcdata_log.write (datetimestr () + ' sql_getlablenames ' + ' >>> ' +sql_getlablenam Es+ ' \ n ') Update_dcdata_log.write (datetimestr () + ' sql_getrecords ' + ' >>> ' +sql_getrecords+ ' \ n ') update_ Dcdata_log.write (DATETIMESTR () + ' sql_getprojectids ' + ' >>> ' +sql_getprojectids+ ' \ n ') Update_dcdata_ Log.write (DATETIMESTR () + ' sql_getlanguagesections ' + ' >>> ' +sql_getlanguagesections+ ' \ n ') Update_dcdata_ Log.write (DATETIMESTR () + ' Sql_addrecordbyid ' + ' >>>' +sql_addrecordbyid+ ' \ n ') Update_dcdata_log.write (datetimestr () + ' Sql_updatelgs ' + ' >>> ' +sql_updatelgs+ ' \ n ') context=context ({' msg ': ' Success '}) resp=render_to_response ("report/clean_data.html", Context, Context_instance=requestcontext (req)) Cursor=connection.cursor () Try:cursor.execute (sql_ Getlablenames) Lablenames=cursor.fetchall () except Exception,e:update_dcdata_log.write (DATETIMESTR () + ' ex Ecute sql_getlablenames error ' +str (e) + ' \ n ') context=context ({' msg ': ' ERROR '}) return Render_to_response ("Rep Ort/clean_data.html ", Context, Context_instance=requestcontext (req)) for Lablename in LA BleNames:try:cursor.execute (Sql_getprojectids,[lablename[0]) Projectids=cursor.fetchall () Except Exception,e:update_dcdata_log.write (DATETIMESTR () + ' Execute sql_getprojectids error ' +str (e) + ' \ n ' ) Context=contexT ({' msg ': ' Error '}) return Render_to_response ("report/clean_data.html", Context, C Ontext_instance=requestcontext (req)) for the PID in ProjectIDs:try:cursor.execute (Sql_getr Ecords,[lablename[0],str (Pid[0]) Records=cursor.fetchall () Cursor.execute (sql_getlanguages Ections,[str (Pid[0]) Languagesections=cursor.fetchall () except EXCEPTION,E:UPD Ate_dcdata_log.write (DATETIMESTR () + ' Execute sql_getrecords or sql_getlanguagesections error ' +str (e) + ' \ n ') Context=context ({' msg ': ' Error '}) return Render_to_response ("report/clean_data.html", Context, Context_instance=requestcontext (req)) values,lgs=[],[] Basevalue=str (records[0][2] ) Baseid=str (records[0][0]) for item in Records:lgs.append (STR (item[1])) Values.append (STR (ITEM[2]) if BASEVALUE!=STR (Item[2]): Basevalue= ' false ' Targetlgs=[str (Item[0]) For item in Languagesections] If Len (LGS) <1 or Len (Targetlgs) <1:basevalue== ' false ' If ' All ' isn't in Lgs:try:cursor.execute (Sql_addrecordbyid,[baseid]) Cursor.execute (sql_updatelgs,[' All ', BaseID]) transaction.commit_unless_managed () E Xcept Exception,e:update_dcdata_log.write (datetimestr () + ' execute Sql_addrecordbyid or Sql_updatelgs er Ror (All) ' +str (e) + ' \ n ') context=context ({' msg ': ' Error '}) return Render_to_response (" Report/clean_data.html ", Context, Context_instance=requestcontext (req)) Update_dcdata_log.write (DATETIMESTR () + "All record is add to Dc_data,the lablename and Projec TID were "+str (lablename[0]) + '-' +str (pid[0]) + ' \ n ') if basevalue== ' false ': Update_dcdata_log.write (datetimestr () + "Please update this record mutually,the Lablename and ProjectID were" +str (lablename[0]) + '-' +str (pid[0]) + ' \ n ') Else:if Len (LGS) >len (TARGETLGS): Update_dcdata_log.write (Datetimestr () + "the Lablenam E languagesection length is longer than target numbers lablename and ProjectID were "+str (lablename[0]) + '-' +str (pid[0]) + ' \ N ') else:for LG in Targetlgs:if LG No in LGS: Try:cursor.execute (Sql_addrecordbyid,[baseid]) c Ursor.execute (Sql_updatelgs,[lg,baseid]) transaction.commit_unless_managed () Except Exception,e:update_dcdata_log.write (DATETIMESTR () + ' Execute Sql_addreco Rdbyid or Sql_updateLgs error (LG) ' +str (e) + ' \ n ') context=context ({' msg ': ' Error '}) Return Render_to_response ("report/clean_data.html", Context, CONTEXT_INSTANCE=REQUESTC Ontext (req)) Update_dcdata_log.write (Datetimestr () +lg+ "record is add to Dc_data,the Lablename and ProjectID were "+str (lablename[0]) + '-' +str (pid[0]) + ' \ n ') Update_dcdata_log.close () Return RESP
Use Django to clean up data in a database