웹에서 긁어 낸 json 데이터에서 관계형 데이터베이스 (다 대다)를 구축하고 있습니다. 두 개의 기본 데이터베이스가 있는데 하나에는 909 개의 행이 있고 다른 하나에는 13 개의 행이 있습니다. json 파일 내에서 링크되는 방법에 따라 큰 테이블의 ID를 교차 테이블의 작은 테이블의 ID와 일치시켜야합니다. 내가 찾은 문제는 이러한 테이블을 함께 관련시키는 것이 없지만 교차 테이블을 채워야한다는 것입니다. 수일이 걸리는 손으로하는 것 외에 내가 시도하거나 생각할 수있는 것은 아무것도 없습니다. 다음은 json 파일의 작은 샘플입니다. 큰 테이블에는 코스 정보가 있고 작은 테이블은 fulfills 키를 사용합니다.
[
{"number": "CHIN 242", "subject": "Chinese", "title": "Chinese Cinema and Chinese Modernity", "description": "From the fall of the Clestial Empire to the rise of China's economy today, Chinese cinema has witnessed many social changes in the modern era. This course will focus on the interaction between Chinese cinema and the process of modernization.", "fulfills": ["Human Expression\u2014Primary Texts", "Intercultural"]},
{"number": "CLAS 240", "subject": "Classics", "title": "Classical Mythology", "description": "A survey of the major myths and legends of ancient Greece and Rome.", "fulfills": ["Human Expression\u2014Primary Texts", "Quantitative"]},
{"number": "CLAS 250", "subject": "Classics", "title": "The World of Ancient Greece", "description": "A historical survey of ancient Greek culture from the Trojan War to the rise of Rome.", "fulfills": ["Human Expression\u2014Primary Texts", "Religion"]},
{"number": "CLAS 255", "subject": "Classics", "title": "Ancient Roman Culture", "description": "This course explores various cultural institutions and practices of the ancient Romans.", "fulfills": ["Human Expression\u2014Primary Texts", "Human Behavior"]},
{"number": "CLAS 265", "subject": "Classics", "title": "Greece and Rome on Film", "description": "This course explores the ways in which various events and episodes from Greek and Roman myth and history have been adapted for modern film and television.", "fulfills": ["Human Expression\u2014Primary Texts"]},
{"number": "CLAS 270", "subject": "Classics", "title": "Archaeology of Ancient Greece", "description": "An in-depth study of the archaeology of ancient Greece, with a focus on the high points of Greek civilization and material culture.", "fulfills": ["Historical", "Human Expression\u2014Primary Texts"]},
{"number": "CLAS 275", "subject": "Classics", "title": "Archaeology of Ancient Rome", "description": "This course explores the archaeology of ancient Rome from its early beginnings to its rapid growth into one of the world's largest empires.", "fulfills": ["Historical", "Human Expression\u2014Primary Texts"]},
{"number": "CLAS 300", "subject": "Classics", "title": "Classics and Culture", "description": "Using texts in translation, this course explores select aspects or themes from the cultures of ancient Greece and Rome.", "fulfills": ["Human Expression\u2014Primary Texts"]},
]
나는 이것에 대한 해결책을 찾았습니다. 교차 테이블을 채우려면 id에 대한 쿼리를 실행하고 작은 테이블과 id 및 숫자와 같은 간단한 것을 수행 한 다음 myDict = dict(map(reversed,cur.fetchall()))
각 쿼리에 대해 실행 합니다. 거기에서 for 루프를 사용하여 json 데이터를 반복 한 다음 중첩 된 for 루프를 사용하여 fulfills 및 cur.execute(insert into ...)
. 다음과 같이 보입니다.
cur.execute('select id, fulfills from reqs;')
dict1 = dict(map(reversed,cur.fetchall()))
cur.execute('select id, number from course;')
dict2 = dict(map(reversed,cur.fetchall()))
for x in geneds:
for y in x:
cur.execute('insert into table(course, req) values(%s, %s);', (dict2[x['number']], dict1[y]))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다