数据库,数据清洗

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

    field:pubdate function=GetDateMK(@me)/] 2009-11-10 [field:pubdate function=GetDateTimeMK(@me)/] 2009 ...

  2. spring3.0注解定时任务配置及说明

    spring注解方式的定时任务配置: 第一步:spring配置文件 <?xml version="1.0" encoding="UTF-8"?> & ...

  3. JDBC批处理executeBatch

    JDBC运行SQL声明,有两个处理接口,一PreparedStatement,Statement,一般程序JDBC有多少仍然比较PreparedStatement 只要运行批处理,PreparedSt ...

  4. HDU ACM 4578 Transformation-&gt;段树-间隔的变化

    分析:复杂的经营分部树. 只有一个查询操作,这是要求[l,r]的数量之间p钍总和.并不是所有的查询所有节点,会议TLE.最好的是查询部件[a.b].所有这个区间值我们是平等的,即能返回(b-a+1)* ...

  5. Android Framework 其中A记录

    一个简短的引论 以往的研究太偏应用层的功能,实现了,原则上不进入非常理解,现在,研究人员framework该框架层. 创纪录的 1.下载源代码,文件夹例如以下: 2.Android系统的层次例如以下: ...

  6. JAVA实现DAO基本层CRUD操作

    随着shh2各种操作方便框架.越来越多JAVA WEB效率,可是,假设在不了解这些框架使用的场合的情况下,一拿到项目就盲目地选择这些框架进行系统架构的搭建,就有可能造成非常多不是必需的资源浪费. 在项 ...

  7. 文件搜索神器everything 你不知道的技巧总结

    everything这个软件用了很久,总结了一些大家可能没注意到的技巧,分享给大家 1.指定文件目录搜索示例: TDDOWNLOAD\ abc        在所有TDDOWNLOAD文件夹下搜索包含 ...

  8. C# WinForm dataGridView 技巧小结

    1.不显示第一个空白列RowHeaderVisible属性设置为false 2.点击cell选取整行SelectinModel属性FullRowSelectRowSelectinModel属性设置或用 ...

  9. ZenCoding for EmEditor Snippets 的安装

    ZenCoding for EmEditor的安装 你可以从这里下载所需文件Library under the Snippets category.安装前请确认你的EmEditor内置有代码片段(Sn ...

  10. dlmalloc 2.8.6 源代码具体解释(6)

    本文章由vector03原创, 转载请注明出处. 邮箱地址: mmzsmm@163.com, 欢迎来信讨论. 3.4 sys_alloc sys_alloc是dlmalloc中向系统获取内存的主要接口 ...