使用Django清理数据库中的数据
数据库,数据清洗
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清理数据库中的数据的更多相关文章
- Django Form 实时从数据库中获取数据
修改 models.py 添加 class UserType(models.Model): caption = models.CharField(max_length=32) 执行命令,生成数据库 p ...
- 将Oracle数据库中的数据写入Excel
将Oracle数据库中的数据写入Excel 1.准备工作 Oracle数据库"TBYZB_FIELD_PRESSURE"表中数据如图: Excel模板(201512.xls): 2 ...
- java更改数据库中的数据
不废话,上代码 package com.ningmeng; import java.sql.*; /** * 1:更改数据库中的数据 * @author biexiansheng * */ publi ...
- Eclipse中java向数据库中添加数据,更新数据,删除数据
前面详细写过如何连接数据库的具体操作,下面介绍向数据库中添加数据. 注意事项:如果参考下面代码,需要 改包名,数据库名,数据库账号,密码,和数据表(数据表里面的信息) package com.ning ...
- C#-WinForm-ListView-表格式展示数据、如何将数据库中的数据展示到ListView中、如何对选中的项进行修改
在展示数据库中不知道数量的数据时怎么展示最好呢?--表格 ListView - 表格形式展示数据 ListView 常用属性 HeaderStyle - "详细信息"视图中列标头的 ...
- phpexcel的写操作将数据库中的数据导入到excel中
这个版本据说是可以支持excel2007,但是我使用2007编辑的xlsx是无法获得该库的支持.于是乎我就将它转化为2003.感觉支持地很好. 下面介绍一下具体的使用: require_once('. ...
- 定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表
最近项目中有一种需求: 大致需求是这样的 通过给定的 用户名和密码 要定时从远程的数据库中取数据,然后把取出来的数据插入或更新本地的oracle数据库的表 项目的结构式struts1 hibernat ...
- 怎样在C#中从数据库中读取数据(数据读取器)
实现在C#中通过语句,查询数据库中的数据 SqlConnection con = null; //创建SqlConnection 的对象 try //try里面放可能出现错误的代码 ...
- 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释
在自学杨中科老师的视频教学时,拓展编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时 会产生Additional information: 阅读器关闭时尝试调用 ...
随机推荐
- ASP.NET MVC源码分析
MVC4 源码分析(Visual studio 2012/2013) HttpModule中重要的UrlRoutingModule 9:this.OnApplicationPostResolveReq ...
- 添加xml文件编辑语法提示
找到Struts的lib目录 找到struts2-core-文件并解压开 这个struts.dtd文件才是我们需要添加的文件 双击XML Catalog 点击ADD Key中复制粘贴D:\web\st ...
- C,C++中的static
在C和C++中,static都有两种基本的含义,并且这两种含义经常是互相有冲突的: 1) 在固定的地址上分配,也就是说对象是在一个特殊的静态数据区上创建的,而不是每次 函数调用时在堆栈上产生的.这也是 ...
- Java调用Lua(转)
Java 调用 Lua app发版成本高,覆盖速度慢,覆盖率页低.一些策略上的东西如果能够从服务端控制会方便一些.所以考虑使用Lua这种嵌入式语言作为策略实现,Java则是宿主语言. 总体上看是一个模 ...
- Word文件交换的电脑打开字体、排版变化的原因和解决方法!
方案: 有时候.我们好不easy用Word写好文档,做好排版发给别人,别人会告诉你格式怎么是乱的啊,标题.正文.页眉页脚什么的格式都变了. 想尽各种办法都没能得到解决,那么出现这样的情况的原因究竟是什 ...
- hdu2377Bus Pass(构建更复杂的图+spfa)
主题链接: 啊哈哈,点我点我 思路: 题目是给了非常多个车站.然后要你找到一个社区距离这些车站的最大值最小..所以对每一个车站做一次spfa.那么就得到了到每一个社区的最大值,最后对每一个社区扫描一次 ...
- WPF技术触屏上的应用系列(三): 视频播放器的使用及视频播放、播放、暂停、可拖动播放进度效果实现
原文:WPF技术触屏上的应用系列(三): 视频播放器的使用及视频播放.播放.暂停.可拖动播放进度效果实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7操作系统,5 ...
- (转)Maven最佳实践:划分模块
“分天下为三十六郡,郡置守,尉,监” —— <史记·秦始皇本纪> 所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块 ...
- 从头开始建网站(三)DNS
前面介绍了站点搭建所须要的两大要素:域名和server,这次要说的是域名解析,也就是把域名和server进行绑定的过程. 我们在訪问网络时,网址会被发送到DNSserver,然后由DNSserver返 ...
- ASP.NET5
ASP.NET5 2015年11月30日, ASP.NET 5 RC1 已经发布,本文尝试了一下ASP.NET5项目的创见一发布到IIS.开发环境,win10 64位,visual studio201 ...