数据库中的数据清理
问题描述:在我所使用的系统中,由于历史和各种原因,导致数据库中表里面记录的数据,是有问题的,有重复的和不完整的数据
解决方案:首先,由于这些数据的量还是挺大的,手工的清理肯定不行,然后,我就想写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!