python 判断更新插入数据库

 1 #-*-coding:utf8-*-
 2 #此处调试代码...
 3 import sys
 4 import os
 5 import MySQLdb
 6 import datetime
 7 path1 = (os.getcwd()).split(r'\test')[0]
 8 sys.path.append(path1+'\Shop_site_crawler')
 9 from zsl_spider_lib import get_u_sql,get_s_sql,get_i_sql
10 import time
11 try:
12     conn=MySQLdb.connect(host='localhost',user='root',passwd='root',charset='utf8')
13     conn.select_db('scrapy')
14 except:
15     conn=MySQLdb.connect(host='rds.com',user='scrapy',passwd='passwd',charset='utf8')
16     conn.select_db('scrapy')
17 print 'task build start :',datetime.datetime.now()
18 
19 #----------------分割线-------------------
20 table = 'shops'
21 curDate = time.strftime("%Y-%m-%d")
22 i = {}
23 i['uid'] = 1314520
24 i['shop_uri'] = 'url'
25 i['shop_name'] = 'name'
26 i['nick'] = 'nick'
27 i['brand'] = 'brand'
28 i['platform'] = 0
29 i['addtime'] = time.strftime("%Y-%m-%d %H:%M:%S")
30 i['the_date'] = time.strftime("%Y-%m-%d")
31 
32 
33 db = conn.cursor()#创建实例
34 conds = {}
35 conds['shop_name'] = i['shop_name']
36 conds['nick'] = i['nick']
37 conds['the_date'] = curDate 
38 
39 keys = ['count(1)']
40 
41 insert_sql = get_i_sql(table,i)
42 select_sql = get_s_sql(table,keys,conds)
43 update_sql = get_u_sql(table,i,conds)
44 
45 db.execute(select_sql)#操作数据库
46 res = db.fetchone()
47 if res[0]:
48     db.execute(update_sql)
49     print '----update reday!'
50 else:
51     db.execute(insert_sql)
52     print '----insert reday!'
53     
54 conn.commit()
55 db.close()
56 conn.close()

 

posted @ 2016-10-31 08:10  恰似少年  阅读(3699)  评论(0编辑  收藏  举报