使用sqlalchemy将记录从json对象插入到表中

2024-05-07 18:14:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编写一个程序,从api调用中获取一个json对象,然后将其加载到数据库中。当我编译stmt或尝试单独执行时,sqlalchemy不断抛出错误AttributeError: 'TextClause' object has no attribute 'implicit_returning'。在

def load_results(self):
      connect = self.engine.connect()

      for ru in self.json_ru:
          counties = ru['counties']
          for county in counties:
              transaction = connect.begin()
              params = {"reportingunitName": ru['reportingunitName'],
                                "geoCode": ru['geocode'],
                                "townsTotal": ru['towns'],
                                "townsReporting": ru['townsReporting'],
                                "cableName": county['cableName'],
                                "unitCount": county['unitCount']
                                }
              stmt = expression.insert('{}.raw_feed'.format(self.state)).values(params)
              compiled_stmt = stmt.compile(self.engine)
              connect.execute(compiled_stmt)
              transaction.commit()

在我提出疑问之前连接.执行,但是作为值传入的字符串被作为列名读取,因此我尝试了上面的方法。也使用Postgres口味。在


Tags: inselfjsonforruconnectparamsengine