数据库,数据清洗

问题叙述性说明:在系统我用在,因为历史和由于各种原因,原因记录的数据内的数据库表,有一个问题,有反复和不完整的数据
解:首先。由于数据量还是挺大的,工的清理肯定不行,
然后,我就想写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


使用Django清理数据库中的数据的更多相关文章

  1. Django Form 实时从数据库中获取数据

    修改 models.py 添加 class UserType(models.Model): caption = models.CharField(max_length=32) 执行命令,生成数据库 p ...

  2. 将Oracle数据库中的数据写入Excel

    将Oracle数据库中的数据写入Excel 1.准备工作 Oracle数据库"TBYZB_FIELD_PRESSURE"表中数据如图: Excel模板(201512.xls): 2 ...

  3. java更改数据库中的数据

    不废话,上代码 package com.ningmeng; import java.sql.*; /** * 1:更改数据库中的数据 * @author biexiansheng * */ publi ...

  4. Eclipse中java向数据库中添加数据,更新数据,删除数据

    前面详细写过如何连接数据库的具体操作,下面介绍向数据库中添加数据. 注意事项:如果参考下面代码,需要 改包名,数据库名,数据库账号,密码,和数据表(数据表里面的信息) package com.ning ...

  5. C#-WinForm-ListView-表格式展示数据、如何将数据库中的数据展示到ListView中、如何对选中的项进行修改

    在展示数据库中不知道数量的数据时怎么展示最好呢?--表格 ListView - 表格形式展示数据 ListView 常用属性 HeaderStyle - "详细信息"视图中列标头的 ...

  6. phpexcel的写操作将数据库中的数据导入到excel中

    这个版本据说是可以支持excel2007,但是我使用2007编辑的xlsx是无法获得该库的支持.于是乎我就将它转化为2003.感觉支持地很好. 下面介绍一下具体的使用: require_once('. ...

  7. 定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表

    最近项目中有一种需求: 大致需求是这样的 通过给定的 用户名和密码 要定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表 项目的结构式struts1 hibernat ...

  8. 怎样在C#中从数据库中读取数据(数据读取器)

    实现在C#中通过语句,查询数据库中的数据 SqlConnection con = null; //创建SqlConnection 的对象 try    //try里面放可能出现错误的代码        ...

  9. 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释

    在自学杨中科老师的视频教学时,拓展编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时 会产生Additional information: 阅读器关闭时尝试调用 ...

随机推荐

  1. http1.0 和 http1.1 区别

    http1.0 和 http1.1 主要区别 1.背景   KeepAlive是就是通常所称的长连接.KeepAlive带来的好处是可以减少tcp连接的开销,这对于短response body的请求效 ...

  2. [java面试题]最长的回文字符串中出现确定

    <span style="font-family: Arial, Helvetica, sans-serif;">package com.wzw.util;</s ...

  3. 如何使用ZEROBRANE STUDIO远程调试COCOS2D-X的LUA脚本(转)

    http://www.cocos2d-x.org/docs/manual/framework/native/v2/lua/lua-remote-debug-via-zerobrane/zh ZeroB ...

  4. Blend4精选案例图解教程(四):请给我路径指引

    原文:Blend4精选案例图解教程(四):请给我路径指引 路径在界面设计中,可以起到很好的辅助作用,我常常使用它来对元素进行规则排列和非规则排列控制. 本次教程将演示,Blend中路径的常规用法. 1 ...

  5. iOS8推送消息的回复处理速度

    iOS8我们有一个新的通知中心,我们有一个新的通报机制.当在屏幕的顶部仅需要接收一个推拉向下,你可以看到高速接口,天赋并不需要输入应用程序的操作.锁定屏幕,用于高速处理可以推动项目. 推送信息,再次提 ...

  6. ProgressBar样式总结与自己主动填充方法(代码)

    有时候开发的时候须要用一个进度条告知用户眼下正在执行一个耗时操作,可是并不须要明白知道某个value来setProgress,所以就能够自己定义一个时间和进度让进度条自己主动执行了. 以下是代码: H ...

  7. AndroidAnnotations说明—AndroidAnnotations它是如何工作的?

    AndroidAnnotations它的工作原理很easy,它使用标准java注塑加工工具,自己主动加她一个额外的步骤生成源代码编译.         源代码是什么?每个增强的类.比方每个用@EAct ...

  8. SecureCRT使用提示

    一旦itpub我写上面,我不知道这个博客的背后,我们无法上传和修改内容.好恼火啊! 原文链接:SecureCRT的几个使用方法设置 在原文的基础上,再补充几个功能: 1.最好将全部设置定制在Globa ...

  9. Apple Watch 1.0 开发介绍 2.1 WatchKit Apps UI要点

    实现app的开始是定义storyboard场景.每个场景定义了app的一部分界面.可以为不同的尺寸自定义场景. 组装storyboard界面 WatchKit app和iOS app的布局模式不同.组 ...

  10. hdu 2454 Degree Sequence of Graph G (推断简单图)

    ///已知各点的度,推断是否为一个简单图 #include<stdio.h> #include<algorithm> #include<string.h> usin ...