Python-Django后台操作数据库

代码操作数据库的时候,可以使用pymysql包,
import pymysql
def connectDB():
    conn = pymysql.connect(host='w.talentin.cn',
                           port=3306, user='case_tracker',
                           passwd='2388dev',
                           db="case_tracker",
                           charset='utf8',
                           cursorclass=pymysql.cursors.DictCursor)  # 可以指定返回的类型,指定为queryset的字典类型,比较方便
    return conn


def tag_person_dbscan():
    """根据person表查询resume表中file_text"""
    conn = connectDB()  # "oceantest"
    with conn.cursor() as cursor:
        person_id = 0
        search_count = 10
        while (search_count > 0):
            search_count = 0
            sql = "select uuid,name,headline from person where uuid>%s limit 10" % person_id  # 在person表中取出十条
            search_count = cursor.execute(sql)
            result = cursor.fetchall()
            for r in result:
                cvs = cv = resume_text = ''
                score = 0

                featuredtags = []
                person_id = r.get("uuid")
                sql = "select resume_id,md5,file_text,uuid from resume where uuid=%s" % person_id  # 根据关联字段uuid-person_id取出resume表中的指定字段
                resume_count = cursor.execute(sql)
                cvs = cursor.fetchall()

                if cvs is not None:
                    for cv in cvs:
                        if cv.get('file_text', '') is not None:  # and cv.searchflag in
                            resume_text = resume_text + cv.get('file_text', '').decode()

                    if resume_text not in (None, ""):
                        params = {'content': resume_text}
                        r = requests.post(
                            "http://w.talentin.cn:1010/tagger/",params)
                        if r.status_code:
                            json_data = json.loads(r.text)['data']['tags']
                            for data in json_data:
                                proof = data['keyword']
                                tag = data['tag_name']
                                probability = data['score']
                                creator = "SmartTagger"
                                now_time = nowTime()  # 现在时间
                                add_person_tag_db(person_id, tag, probability, proof, creator, now_time)  # 添加到tag_person表

                        # score, featuredtags = tag_person_API(resume_text)  # 云端算法服务
                        # if score >= threshhold:
                        #     print("[Bingo]%s , p=%0.2f \n %s" % (person_id, score, featuredtags))
                        #     tag_person_db(person_id, person_tag_str, score, featuredtags, creator_tag)
                        #     continue
                        # else:
                        #     pass
        cursor.close()
    conn.close()





conn = connectDB()
with conn.cursor() as cursor:
    search_count=1

    while(search_count):
        result=''
        r=''
        sql="select uuid,resume_id,md5,file_binary from resume "
        sql=sql+'where resume_id <20000 and resume_id >12000 and searchflag!=8 and searchflag!=10 and searchflag!=11 and searchflag !=13 and searchflag!=14  limit 10'
        search_count=cursor.execute(sql)
        #print("*****",search_count)
        result= cursor.fetchall()

        for r in result:
              if r.get("file_binary") is not None:  
                  text=r.get("file_binary")
                  source_encoding = chardet.detect(text)['encoding']
                  if  source_encoding is not None:    
                  
                      if source_encoding in 'utf-8':
                              print(r.get("resume_id"),source_encoding,' 8')
                              UpdateResumeDB(r.get("resume_id"),'searchflag','8')
                      elif source_encoding not in 'UTF-8-SIG':
                              print(r.get("resume_id"),source_encoding,' 10')
                              UpdateResumeDB(r.get("resume_id"),'searchflag','10')
                      elif source_encoding not in 'GB2312':
                              print(r.get("resume_id"),source_encoding,' 11')
                              UpdateResumeDB(r.get("resume_id"),'searchflag','11')    
                      else:
                              print(r.get("resume_id"),source_encoding,' 13')
                              UpdateResumeDB(r.get("resume_id"),'searchflag','13')
                  else:
                      print(r.get("resume_id"),source_encoding,' 13')
                      UpdateResumeDB(r.get("resume_id"),'searchflag','13')

                      
              else:
                  print(r.get("resume_id"),"None", ' 14')
                  UpdateResumeDB(r.get("resume_id"),'searchflag','14')
                      
              conn.commit()   
    cursor.close()
conn.close()


猜你喜欢

转载自blog.csdn.net/weixin_40475396/article/details/79925457