【发布时间】:2020-02-12 16:28:39
【问题描述】:
下面的脚本反映了我更新、编辑的尝试(遵循以下建议)使用操作数据库中的表中的行填充维度表,前提是 PANDAS DataFrame 的主键是通过连接 OPDB 中相关表的 ID 列创建的,维度表中不存在。
import mysql.connector
import pandas as pd
...
op_cursor = op_connector.cursor
dwh_cursor = dwh_connector.cursor
...
class dimension_table:
def __init__(self, dwh_cols, op_cols, dim_id, dwh_table_name, op_table_name,op_args=None, dwh_args=None):
self.dwh_cols = ('')
self.op_cols = ('')
self.dim_id = dim_id
self.dwh_table_name = dwh_table_name
self.op_table_name = '`*opdb.*`.' + op_table_name
self.op_args = ",".join(op_cols)
self.dwh_args = ",".join(dwh_cols)
...
billing_address_data = dimension_table(("id","address", "alias", "postal_code", "type", "city", "country",
"geolocation"),
("id","address", "alias", "postal_code", "type", "city", "country",
"geolocation"),
billing_address_dim_id,'billing_address_dim', 'billing_address')
...
def load_dim(instance):
sql = """INSERT INTO {dwh} ({dwh_cols})
SELECT {op_cols}
FROM {op}
WHERE {pk} NOT IN
(SELECT {pk} FROM {dwh} WHERE id = %s)
LIMIT 1
"""
for key in instance.dim_id:
try:
# ID APPEND
dwh_cursor.execute(sql.format(dwh = instance.dwh_table_name,
dwh_cols = instance.dwh_args,
op_cols = instance.op_args,
op = instance.op_table_name,
pk = 'id'),
str(key))
dwh_connector.commit()
except mysql.connector.ProgrammingError as err:
# ORDER_ID APPEND
dwh_cursor.execute(sql.format(dwh = instance.dwh_table_name,
dwh_cols = instance.dwh_args,
op_cols = instance.op_args,
op = instance.op_table_name,
pk = 'order_id'),
str(key))
dwh_connector.commit()
billing_profile_op_id = dwh_cursor.lastrowid
...
load_dim(order_items_data)
我的最新问题是由于在脚本中运行最后一行代码导致的错误,load_dim(order_items_data)。
它是带有 order_id PK 的 order_items 表。
ProgrammingError: 1054 (42S22): 'where 子句'中的未知列 'id'
【问题讨论】:
-
请在您的问题中添加minimal reproducible example,以及遇到的具体错误。
-
请为有问题的 SQL 表添加示例数据。还包括您当前的 Python 代码。
-
你试过
WHERE id = '"+str(key)+"' OR order_id = '"+str(key)+"'吗? -
请添加表结构或一些示例数据。如果没有有关架构或数据的信息,很难猜测。
-
@LeoGer 。 . .只是您问这表明您的数据模型存在问题。您应该将它们存储在单独的行中,而不是将重复值存储在单独的列中。