psycopg2插入嵌套词典列表中的多个表格

发布于 2025-01-23 01:11:12 字数 1910 浏览 5 评论 0原文

因此,我有嵌套词典的列表,这些字典从我想获取值并将其插入我的PostgreSQL 14数据库中的表中。我正在与Psycopg2一起使用Python。

列表看起来像这样...

嵌套词典列表

[
 {
  "trainNumber": 1,
  "departureDate": "2020-12-08",
  "cancelled": false,
  "trainType": "IC",
  "0": {
   "type": "DEPARTURE",
   "stationShortCode": "HKI",
   "scheduledTime": "2020-12-08T04:57:00.000Z"
  },
  "1": {
   "type": "ARRIVAL",
   "stationShortCode": "PSL",
   "scheduledTime": "2020-12-08T05:02:00.000Z"
  },
  "2": {
   "type": "DEPARTURE",
   "stationShortCode": "PSL",
   "scheduledTime": "2020-12-08T05:03:00.000Z"
  },
{
  "trainNumber": 2,
  "departureDate": "2020-12-08",
  "cancelled": false,
  "trainType": "S",
  "0": {
   "type": "DEPARTURE",
   "stationShortCode": "JNS",
   "scheduledTime": "2020-12-08T03:12:00.000Z"
  },
  "1": {
   "type": "ARRIVAL",
   "stationShortCode": "PLT",
   "scheduledTime": "2020-12-08T03:12:34.000Z"
  },
  "2": {
   "type": "DEPARTURE",
   "stationShortCode": "PLT",
   "scheduledTime": "2020-12-08T03:12:34.000Z"
  },

psycopg2 的查询

insert_query = WITH test AS (
    INSERT INTO trains_data.trains (train_number, train_type)
    VALUES (%(trainNumber)s, %(trainType)s)
    RETURNING id
), test2 as (
    INSERT INTO trains_data.train_routes (train_id, date)
    VALUES ( (SELECT id FROM test), %(departureDate)s)
    RETURNING id
), test3 as (
    INSERT INTO trains_data.train_timetables (route_id, scheduled_time, station_id, route_type, cancelled)
    VALUES ( (SELECT id FROM test2), %(scheduledTime)s, (SELECT station_id FROM trains_data.train_stations WHERE short_name = %(stationShortCode)s), %(type)s, %(cancelled)s)
);

问题是,当我运行此查询时,我会得到keyError:'scheduledtime'> 因此,我的问题是:

  1. 如何访问需要获得时间,类型和站点值值的嵌套词典,还是有可能?
  2. 我应该考虑一些不同的方法来插入我的数据吗?
  3. 我的SQL代码正确插入数据有些正确吗?

任何有点帮助都将不胜感激。

So i have list of nested dictionaries which from i want to get values and insert them into tables on my Postgresql 14 database. I'm using Python with Psycopg2.

List looks like this...

List of nested dictionaries

[
 {
  "trainNumber": 1,
  "departureDate": "2020-12-08",
  "cancelled": false,
  "trainType": "IC",
  "0": {
   "type": "DEPARTURE",
   "stationShortCode": "HKI",
   "scheduledTime": "2020-12-08T04:57:00.000Z"
  },
  "1": {
   "type": "ARRIVAL",
   "stationShortCode": "PSL",
   "scheduledTime": "2020-12-08T05:02:00.000Z"
  },
  "2": {
   "type": "DEPARTURE",
   "stationShortCode": "PSL",
   "scheduledTime": "2020-12-08T05:03:00.000Z"
  },
{
  "trainNumber": 2,
  "departureDate": "2020-12-08",
  "cancelled": false,
  "trainType": "S",
  "0": {
   "type": "DEPARTURE",
   "stationShortCode": "JNS",
   "scheduledTime": "2020-12-08T03:12:00.000Z"
  },
  "1": {
   "type": "ARRIVAL",
   "stationShortCode": "PLT",
   "scheduledTime": "2020-12-08T03:12:34.000Z"
  },
  "2": {
   "type": "DEPARTURE",
   "stationShortCode": "PLT",
   "scheduledTime": "2020-12-08T03:12:34.000Z"
  },

Query for Psycopg2

insert_query = WITH test AS (
    INSERT INTO trains_data.trains (train_number, train_type)
    VALUES (%(trainNumber)s, %(trainType)s)
    RETURNING id
), test2 as (
    INSERT INTO trains_data.train_routes (train_id, date)
    VALUES ( (SELECT id FROM test), %(departureDate)s)
    RETURNING id
), test3 as (
    INSERT INTO trains_data.train_timetables (route_id, scheduled_time, station_id, route_type, cancelled)
    VALUES ( (SELECT id FROM test2), %(scheduledTime)s, (SELECT station_id FROM trains_data.train_stations WHERE short_name = %(stationShortCode)s), %(type)s, %(cancelled)s)
);

Problem is that when i run this query i get KeyError: 'scheduledTime'
So my questions are:

  1. How can i access nested dictionaries where i need to get time, type and stationShortCode values or is it even possible?
  2. Should i consider some different kinda approach to insert my data?
  3. Is my SQL code somewhat correct to insert data correctly?

Any kinda help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文