1 #-*-coding:utf8-*-2 #Debug code here ...3 ImportSYS4 ImportOS5 ImportMySQLdb6 Importdatetime7path1 = (OS.GETCWD ()). Split (R'\test') [0]8Sys.path.append (path1+'\shop_site_crawler')9 fromZsl_spider_libImportGet_u_sql,get_s_sql,get_i_sqlTen Import Time One Try: AConn=mysqldb.connect (host='localhost', user='Root', passwd='Root', charset='UTF8') -conn.select_db (' Scrapy') - except: theConn=mysqldb.connect (host='rds.com', user='scrapy', passwd='passwd', charset='UTF8') -conn.select_db ('scrapy') - Print 'Task build Start:', Datetime.datetime.now () - + #----------------Split Line------------------- -Table ='Shops' +Curdate = Time.strftime ("%y-%m-%d") Ai = {} ati['UID'] = 1314520 -i['Shop_uri'] ='URL' -i['Shop_name'] ='name' -i['Nick'] ='Nick' -i['Brand'] ='Brand' -i['Platform'] =0 ini['Addtime'] = Time.strftime ("%y-%m-%d%h:%m:%s") -i['the_date'] = Time.strftime ("%y-%m-%d") to + -db = Conn.cursor ()#Create an instance theConds = {} *conds['Shop_name'] = i['Shop_name'] $conds['Nick'] = i['Nick']Panax Notoginsengconds['the_date'] =curdate - theKeys = ['count (1)'] + AInsert_sql =get_i_sql (table,i) theSelect_sql =get_s_sql (table,keys,conds) +Update_sql =get_u_sql (table,i,conds) - $Db.execute (Select_sql)#manipulating Databases $res =Db.fetchone () - ifRes[0]: - Db.execute (update_sql) the Print '----Update reday!' - Else:Wuyi Db.execute (insert_sql) the Print '----Insert reday!' - Wu Conn.commit () - db.close () AboutConn.close ()
Python Judgment Update Insert Database