Estoy construyendo una base de datos relacional (muchos a muchos) a partir de datos json extraídos de la web. Tengo dos bases de datos principales, una con 909 filas y la otra con 13 filas. Necesito hacer coincidir las identificaciones de la tabla grande con las identificaciones de la tabla pequeña en la tabla de intersección, correspondientes a cómo están vinculadas dentro del archivo json. El problema que encuentro es que no hay nada que relacione estas tablas, pero necesito completar la tabla de intersección. No hay nada que intente o se me ocurra que pueda poblarlo, aparte de hacerlo a mano, lo que llevará días. A continuación se muestra una pequeña muestra del archivo json. La tabla grande tiene información del curso y la tabla pequeña usa la tecla de cumplimiento.
[ {"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"]}, ]Encontré la solución a esto. Para llenar la tabla de intersección, ejecute una consulta en el id y complete en la tabla pequeña, e id y algo simple como número, luego ejecute myDict = dict(map(reversed,cur.fetchall())) para cada consulta. a partir de ahí, use un bucle for para iterar los datos json, y luego un bucle for anidado para iterar los elementos de la lista en los cumplimientos y cur.execute(insert into ...) . Se parece a esto:
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]))