使用Django来清理数据库中的数据
来源:互联网 发布:蓝牙鼠标怎么连接mac 编辑:程序博客网 时间:2024/05/22 04:46
数据库中的数据清理
问题描述:在我所使用的系统中,由于历史和各种原因,导致数据库中表里面记录的数据,是有问题的,有重复的和不完整的数据
解决方案:首先,由于这些数据的量还是挺大的,手工的清理肯定不行,
然后,我就想写SQL脚本来按照约定的规则进行更新,可以利用游标
来完成表中的记录的遍历,但是SQL是面向结构化的查询语言,不是面向过程的,所以虽然可以但是没有C和python这样的面向过程的使用方便,
后来,我想直接在我的项目中新建一个方法,然后通过浏览器的地址栏来调用,就可以了。
PS:虽然说Django的orm很方便,但是自己使用起来还是非常的尴尬,一些筛选条件和语法规则,我还是得在网上查找例子才知道怎么用
幸好提供了直接执行SQL语句方法,我在清理的过程中,用的就是执行原生的SQL语句。
代码如下:
def datetimestr(): return datetime.now().strftime('%Y%m%d-%H%M%S')+'>>>' def update_dcData(req): log_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 by project_id) a,' \ ' (select count(*) num ,project_id from management_project_target_lang group by project_id) b' \ ' where a.project_id=b.project_id and a.num!=b.num order by project_id' sql_getAllProjectIDs='select project_id from management_project' sql_getLanguageSections='select b.name from management_project_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 lableName =%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,project_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 "%%_all" group by lableName' update_dcData_log.write(datetimestr()+'sql_getLableNames'+'>>>'+sql_getLableNames+'\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()+'execute sql_getLableNames error '+str(e)+'\n') context=Context({'msg':'Error'}) return render_to_response("report/clean_data.html", context, context_instance=RequestContext(req)) for lableName in lableNames: 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, context_instance=RequestContext(req)) for pid in projectIDs: try: cursor.execute(sql_getRecords,[lableName[0],str(pid[0])]) records=cursor.fetchall() cursor.execute(sql_getLanguageSections,[str(pid[0])]) languageSections=cursor.fetchall() except Exception,e: update_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' not in lgs: try: cursor.execute(sql_addRecordByID,[baseID]) cursor.execute(sql_updateLgs,['all',baseID]) transaction.commit_unless_managed() except Exception,e: update_dcData_log.write(datetimestr()+'execute sql_addRecordByID or sql_updateLgs error (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 into dc_data,the lableName and projectID 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 lableName 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 not in lgs: try: cursor.execute(sql_addRecordByID,[baseID]) cursor.execute(sql_updateLgs,[lg,baseID]) transaction.commit_unless_managed() except Exception,e: update_dcData_log.write(datetimestr()+'execute sql_addRecordByID or sql_updateLgs error (lg)'+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()+lg+" record is add into dc_data,the lableName and projectID were "+str(lableName[0])+'-'+str(pid[0])+'\n') update_dcData_log.close() return resp
0 0
- 使用Django来清理数据库中的数据
- Django 使用admin来管理数据库中的内容
- django查询两个数据库来显示数据
- 使用“软删除”来删除数据库中的数据
- 使用jUnit和JDBC来读取数据库中的数据
- django项目中的数据库中增加数据
- 在scrapy中使用django model来存储数据
- Django学习笔记(三)Django使用admin管理界面来操作mysql数据库
- 使用Java来处理C++存储在数据库中的Blob数据。
- 电子商务使用数据加密来保护数据库
- 电子商务使用数据加密来保护数据库
- 电子商务使用数据加密来保护数据库
- 使用sqlite数据库来管理数据
- Django 中的数据库操作
- django中的数据库配置
- Django中的数据库操作
- 使用文件来保存程序中的数据
- 清理RMAN备份数据中的过期数据
- Android百度地图开发之开发第一个小应用,显示基础的地图页面
- 【bzoj 2750】: [HAOI2012]Road
- Perl 元字符
- Linux 下建立VPN客户端
- SEAJS 的使用 – 配置,及版本号控制
- 使用Django来清理数据库中的数据
- oc_study27
- Android中应该注意的细节
- 黑马程序员——C语言------指针和指针变量
- 官网MapReduce实例代码详细批注
- 黑马程序员——Java开发前奏 JDK Eclipse Tomcat的安装与配置
- easeljs web开发围住神经小猫咪【三】
- Android加载网络图片学习过程,涉及到本地缓存
- Android View绘制流程