MCPcopy Index your code
hub / github.com/KnowledgeXLab/LeanRAG / insert_data_to_mysql

Function insert_data_to_mysql

database_utils.py:133–220  ·  view source on GitHub ↗
(working_dir)

Source from the content-addressed store, hash-verified

131 con.close()
132
133def insert_data_to_mysql(working_dir):
134 dbname=os.path.basename(working_dir)
135 db = pymysql.connect(host='localhost',port=4321, user='root',
136 passwd='123',database=dbname, charset='utf8mb4')
137 cursor = db.cursor()
138
139 entity_path=os.path.join(working_dir,"all_entities.json")
140 with open(entity_path,"r")as f:
141 val=[]
142 for level,entitys in enumerate(f):
143 local_entity=json.loads(entitys)
144 if type(local_entity) is not dict:
145 for entity in json.loads(entitys):
146 # entity=json.load(entity_l)
147
148 entity_name=entity['entity_name']
149 description=entity['description']
150 # if "|Here" in description:
151 # description=description.split("|Here")[0]
152 source_id="|".join(entity['source_id'].split("|")[:5])
153
154 degree=entity['degree']
155 parent=entity['parent']
156 val.append((entity_name,description,source_id,degree,parent,level))
157 else:
158 entity=local_entity
159 entity_name=entity['entity_name']
160 description=entity['description']
161 source_id="|".join(entity['source_id'].split("|")[:5])
162 degree=entity['degree']
163 parent=entity['parent']
164 val.append((entity_name,description,source_id,degree,parent,level))
165 sql = "INSERT INTO entities(entity_name, description, source_id, degree,parent,level) VALUES (%s,%s,%s,%s,%s,%s)"
166 try:
167 # 执行sql语句
168 cursor.executemany(sql,tuple(val))
169 # 提交到数据库执行
170 db.commit()
171 except Exception as e:
172 # 发生错误时回滚
173 db.rollback()
174 print(e)
175 print("insert entities error")
176
177 relation_path=os.path.join(working_dir,"generate_relations.json")
178 with open(relation_path,"r")as f:
179 val=[]
180 for relation_l in f:
181 relation=json.loads(relation_l)
182 src_tgt=relation['src_tgt']
183 tgt_src=relation['tgt_src']
184 description=relation['description']
185 weight=relation['weight']
186 level=relation['level']
187 val.append((src_tgt,tgt_src,description,weight,level))
188 sql = "INSERT INTO relations(src_tgt, tgt_src, description, weight,level) VALUES (%s,%s,%s,%s,%s)"
189 try:
190 # 执行sql语句

Callers 2

hierarchical_clusteringFunction · 0.90
database_utils.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected