简体   繁体   中英

Insert dictionary into Postgres via SQLAlchemy

I'm trying to load a dictionary into a Postgres database via SQLAlchemy in Python. The dictionary as it current exists is:

FinalData = {'GAME_ID': {0: '0021600457', 1: '0021600457', 2: '0021600457', 3: 
'0021600457', 4: '0021600457', 5: '0021600457'}, 'TEAM_ID': {0: 1610612744, 1: 
1610612744, 2: 1610612744, 3: 1610612744, 4: 1610612744, 5: 1610612744}, 
'TEAM_ABBREVIATION': {0: 'GSW', 1: 'GSW', 2: 'GSW', 3: 'GSW', 4: 'GSW', 5: 
'GSW'}}

I have the following code written (based off other tutorials/questions I've found here and online). I could make this work if I was only inserting 1 records - since I could manually assign the attribute to PlayerStats. But with the dictionary having multiple records, I don't know how to bulk pass them all onto the session statements.

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String

Base = declarative_base()

class PlayerStats(Base):
    __tablename__ = 'PlayerStats'

    GAME_ID = Column(String(10), primary_key=True)
    TEAM_ID = Column(String(10))
    TEAM_ABBREVIATION = Column(String(8))

###################################################
## I DON'T KNOW WHAT GOES HERE TO BRIDGE THE GAP ##
###################################################

engine = create_engine('postgres://.........')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
session.add(FinalData)  # I'M ASSUMING I NEED TO ITERATE OVER THIS #

I'm sure I'm missing something simple, but how do I pass all contents of the FinalData dictionary onto the SQLAlchemy add statement?

Your FinalData structure is made of 3 inner dicts, each one keyed by some integer sequence, so you have to unpack it by creating a new PlayerStats object for each key in the subdicts.

In the example below I'm using the integer keys of the FinalData['GAME_ID'] inner dict to search for values in the other two dicts to fill the remaining fields.

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String

Base = declarative_base()

class PlayerStats(Base):
    __tablename__ = 'PlayerStats'

    GAME_ID = Column(String(10), primary_key=True)
    TEAM_ID = Column(String(10))
    TEAM_ABBREVIATION = Column(String(8))

###################################################
FinalData = {'GAME_ID': {0: '0021600457', 1: '0021600457', 2: '0021600457', 3: 
'0021600457', 4: '0021600457', 5: '0021600457'}, 'TEAM_ID': {0: 1610612744, 1: 
1610612744, 2: 1610612744, 3: 1610612744, 4: 1610612744, 5: 1610612744}, 
'TEAM_ABBREVIATION': {0: 'GSW', 1: 'GSW', 2: 'GSW', 3: 'GSW', 4: 'GSW', 5: 
'GSW'}}
###################################################

engine = create_engine('postgres://.........')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

for key in FinalData['GAME_ID']:
    p = PlayerStats()
    p.GAME_ID = FinalData['GAME_ID'][key]
    p.TEAM_ID = FinalData['TEAM_ID'].get(key)
    p.TEAM_ABBREVIATION = FinalData['TEAM_ABBREVIATION'].get(key)
    session.add(p)

session.commit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM