Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过SQLAlchemy将字典插入Postgres_Python_Postgresql_Dictionary_Sqlalchemy - Fatal编程技术网

Python 通过SQLAlchemy将字典插入Postgres

Python 通过SQLAlchemy将字典插入Postgres,python,postgresql,dictionary,sqlalchemy,Python,Postgresql,Dictionary,Sqlalchemy,我正试图通过Python中的SQLAlchemy将字典加载到Postgres数据库中。当前存在的词典是: FinalData = {'GAME_ID': {0: '0021600457', 1: '0021600457', 2: '0021600457', 3: '0021600457', 4: '0021600457', 5: '0021600457'}, 'TEAM_ID': {0: 1610612744, 1: 1610612744, 2: 1610612744, 3: 1610612

我正试图通过Python中的SQLAlchemy将字典加载到Postgres数据库中。当前存在的词典是:

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'}}
我根据在这里和网上找到的其他教程/问题编写了以下代码。如果我只插入1条记录,我就可以做到这一点——因为我可以手动将属性分配给PlayerStats。但是由于字典有多条记录,我不知道如何将它们全部批量传递到会话语句中

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 #

我确信我遗漏了一些简单的内容,但如何将FinalData字典的所有内容传递到SQLAlchemy add语句?

您的FinalData结构由3个内部dict组成,每个dict都由一些整数序列设置了键,因此您必须通过为子部分中的每个键创建一个新的PlayerStats对象来解压它

在下面的示例中,我使用FinalData['GAME_ID']内部dict的整数键来搜索其他两个dict中的值,以填充其余字段

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()

伟大的非常感谢。就最佳实践而言,将内部词典解包并将其翻译成一个大词典是否更有意义?如果是这样的话,是否有一个正确的过程来这样做?@GeorgeRodman不,我认为你不必在处理字典之前转换它,除非你是生成字典的人,并且可以以不同的方式生成它。否则,只需按原样接受它,并将其直接转换为数据库对象,如上图所示。中间结构只会增加混乱。