python sqlite的使用心得 _insert
作者:互联网
def jzbm_insert(p1): # arr_col=['受种者编码', '受种者姓名', '受种者性别', '出生日期', '身份证', '电话号码', '住址', '工作单位', '人群分类', '人群分类2'] key="受种者编码" tablename="user" # con1=create_engine("sqlite:///db/test.db") if con1.has_table(tablename)==False: p2=pd.DataFrame(columns=arr_col) for q in arr_col: if q in p1.columns: p2[q]=p1[q] p2.to_sql(tablename,con1,index=False) else: # p2=pd.DataFrame() for q in arr_col: if q in p1.columns: p2[q]=p1[q] # if key in p2.columns: str1=list_to_string(p2[key].to_list()) sql1=''' select {0} from {1} WHERE {0} IN ({2}) '''.format(key,tablename,str1) d1=pd.read_sql(sql1,con1) # p21=p2.loc[ p2[key].isin(d1[key]) ] for q in p21.index: ps21=p21.loc[q] str1="" for w in p21.columns: if (w!=key)&(pd.isna(ps21[w])==False): if str1=="": str1="{0}='{1}'".format(w,ps21[w]) else: str1=str1+",{0}='{1}'".format(w,ps21[w]) sql2=''' UPDATE {0} SET {1} WHERE {2}='{3}'; '''.format(tablename,str1,key,ps21[key]) con1.execute(sql2) #break # p22=p2.loc[ (p2[key].isin(d1[key]))==False ] if len(p22)>0: p22.to_sql(tablename,con1,if_exists="append",index=False)
标签:p2,insert,sqlite,ps21,con1,python,str1,tablename,key 来源: https://www.cnblogs.com/lsz3034/p/16184252.html