基于python的数据库同步_一个python做得数据库同步

标签:

最近在做python的项目,那么今天也来介绍下python的一个项目。首先先讲一下python的特点:其实python本身主要不是为了网络开发而设计的。至于现在很多web开发python用得广泛,主要还是因为python起步容易,上手快,代码易读性很高。但是值得一说的是,真正用python做项目时,其逻辑也很复杂,并不都是那么简单。这里介绍一个数据库同步的python项目:

#-*- coding: utf-8 -*-#‘‘‘#Created on 2015-1-5#@author: lianghongwei#‘‘‘

importdatetime,sys

sys.path.append(‘/home/lianghongwei/test/src/information_platform‘)from settings importMYSQL_ROOT_PASSWORDfrom base.utils.json_utils importjson_loadsfrom base.utils.mysql_utils importget_cursorclassDataRsync:def __init__(self,the_date):

self.game_list_url= ‘http://bugu.163.com/UG/AgentGameList‘ #获取游戏列表的链接,参数gid,为-1时,返回id为最大的10个游戏

self.game_info_url = ‘http://bugu.163.com/UG/AgentGameInfo‘ #参数gid,type(1代表GAC,2代表SDC) POST的

#获取数据库连接游标

#通过ajax从外部链接访问到数据

def __post_get_data(self,url,para):importurllib,urllib2

params=urllib.urlencode(para)#print url,params

response =urllib2.urlopen(url,params)

data_string=response.read()#print data_string

returnjson_loads(data_string)

#数据的处理,这里是拿到数据后的处理函数,后面会重点讲这个方法def __save_data(self,data_id):

cur=get_cursor(‘info_app‘)try:ifdata_id:

cur.execute(‘select id from industry_library_agent_game where name=%s‘,data_id[‘game_name‘])#industry_library_agent_game_review

result=cur.fetchone()ifresult:

data= self.__post_get_data(self.game_info_url,{‘gid‘:data_id[‘gid‘],‘type‘:1})#industry_library_agent_game_review

values =[

data[‘info‘][‘inno‘],data[‘info‘][‘inno_res‘],

data[‘info‘][‘core‘],data[‘info‘][‘core_res‘],

data[‘info‘][‘biaoxia‘],data[‘info‘][‘biaoxian_res‘],

data[‘info‘][‘consume‘],data[‘info‘][‘consume_res‘],

data[‘info‘][‘shangshou‘],data[‘info‘][‘shangshou_res‘],

data_id[‘gid‘]]

sql=‘update industry_library_agent_game_review set bg_inno_res=%s,bg_core_res=%s,bg_biaoxian_res=%s,bg_consume_res=%s,bg_shangshou_res=%s where id=%d‘cur.execute(sql,values)printvalueselse:print data_id[‘game_name‘]#cur.executemany(‘insert into industry_library_agent_game (bg_info,bg_nno) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)‘,values)

except:pass

def __resave_data(self,fid):

data_id=self.__post_get_data(self.game_list_url,{‘gid‘:fid})#print data_id

for i in range(len(data_id[‘info‘])):#print len(data_id[‘info‘])

printi

self.__save_data(data_id[‘info‘][i])if i<9:returnFalseelse:print 666self.__resave_data(data_id[‘info‘][i][‘gid‘])

#calData方法,用来访问数据defcalcData(self):

data_id=self.__post_get_data(self.game_list_url,{‘gid‘:-1})for i in range(len(data_id[‘info‘])):#print len(data_id[‘info‘])

self.__save_data(data_id[‘info‘][i])

self.__resave_data(data_id[‘info‘][i][‘gid‘])

#代码运行入口if __name__ == "__main__":

now= datetime.date(2013,7,9)

t=DataRsync(now)

t.calcData()

那么这个项目本身很简单,但是其实用到的东西并不少,首先,是加载模块,python加载模块跟c++很像,都是引入文件名,from  A import B其实就是在a文件中引入B模块。很好理解吧。接下来是我们构造一个类,并在类中创建方法,第一个方法是通过ajax访问外部数据,这里也很简单,调用urlopen这个api,访问到数据。接下来是函数的核心部分,拿到数据并处理。这里用sql语句就好,拿到后也很简单,这里只有 一个很简单的逻辑,就是判断是否已经存在于我方数据库,如果存在,则updata,不存在就不更新。接下来是入口函数,调用我们的类里面的方法。最后是执行入口。这是python里面一个很简单的程序,简单易懂。但是如果在做大项目时,尤其用到django框架的时候,项目就没那么简单了!

标签:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值