def store_data(id_name,sq_data):
"""将获取到的数据比较并插入到sqlite"""
#连接数据库
cx = sqlite.connect('chaojiv1.1.sqlite')
cu = cx.cursor()
#raw_input("Press ENTER to exit")
#0,1、2、3、4别为min/max/mostnum/mostprice/price/
cu.execute('''create table if not exists kaixin(
id text primary key,
name text,
min float,
max float,
mostpricenum text,
mostprice float,
price float,
rate float,
maxrate float,
categ text
)''')
#查询比较并写入数据库
#rc='replace into kaixin values(' +pp[jj]+',"'+bb[jj]+'",'+xmlprice[-1]+','+max(xmlprice)+','+min(xmlprice)+')'
for i in id_name[0].keys():
rc='select min, max, mostpricenum,mostprice from kaixin where id='+i
#mostprice float, mostpricenum text, maxrate float
cu.execute(rc)
oo=cu.fetchall()
#c为tuple,0,1、2、3、4别为min/max/mostnum/mostprice/price/
temp0=sq_data[0]
temp1=sq_data[1]
temp2=sq_data[2]
temp3=sq_data[3]
temp4=sq_data[4]
if len(oo)==0:
print "new item",id_name[0].decode('utf-8').encode('gbk')
else:
if temp0>oo[0][0]:
temp0=oo[0][0]
if temp1<oo[0][1]:
temp1=oo[0][1]
if temp2<oo[0][2]:#比较多出现的价格次数,次数少就按照多出现的价格
temp2=oo[0][2]
temp3=oo[0][3]
#print rc
rc='replace into kaixin values(' +i+',"'+id_name[0]+'",'+str(temp0)+','+str(temp1)+ \
','+str(temp2)+','+str(temp3)+',' +str(temp4)+','+str((float(temp1)-float(temp4))/float(temp4)*100)+\
','+ str((float(temp3)-float(temp4))/float(temp4)*100)+',"'+id_name[1] + '")'
#print rc
cu.execute(rc)
cx.commit()
cx.close()