简体   繁体   English

使用 Python 将 JSON 数据插入 Oracle

[英]Insert JSON data into Oracle using Python

everybody.大家。 I'm having problems working with json files.我在处理 json 文件时遇到问题。 I have such a list of Json data, how can I insert this data to a table?我有这样一个 Json 数据列表,如何将这些数据插入到表中? Python code: Python代码:

with open('object.json') as f:
    json_obj = json.load(f)
print(json_obj)

for i in enumerate(json_obj):
    id = validate_string(item.get("id", None))
    LAST_NAME = validate_string(item.get("LAST_NAME", None))
    FIRST_NAME = validate_string(item.get("FIRST_NAME", None))
cursor.execute("insert into EMPLOYEES (id,LAST_NAME,FIRST_NAME) VALUES (:1,:2,:3)", (id,LAST_NAME,FIRST_NAME))
conn.close

JSON some dates in the (object.json) file: JSON (object.json) 文件中的一些日期:

[{"ID": 1, "LAST_NAME": "Alex", "FIRST_NAME": "Pip"}, 
{"ID": 2, "LAST_NAME": "John", "FIRST_NAME": "Alan"},
{"ID": 3, "LAST_NAME": "Dehan", "FIRST_NAME": "Luck"},
{"ID": 4, "LAST_NAME": "Nick", "FIRST_NAME": "Adem"},
{"ID": 5, "LAST_NAME": "Aspen", "FIRST_NAME": "Turit"}]

DatabaseError: ORA-00904: "ID": invalid identifier DatabaseError:ORA-00904:“ID”:无效标识符

ORA-904 is "invalid identifier". ORA-904 是“无效标识符”。

  • Are you sure the column id exists?您确定列 id 存在吗?
  • Could it be you created the employee table with case sensititive column names, I see that in your insert statement you have "id" in lower case and "LAST_NAME" in upper case.您是否使用区分大小写的列名创建了员工表,我看到在您的插入语句中,您有小写的“id”和大写的“LAST_NAME”。 If your column names are case sensitive you have to surround them with double quotes.如果您的列名区分大小写,则必须用双引号将它们括起来。 Example:例子:
create table test ("id" NUMBER, "Name" VARCHAR2(100));
INSERT INTO test (id, name) VALUES (1,'KOEN'); 
> gives ORA-00904: "NAME": invalid identifier 
INSERT INTO test ("id", "Name") VALUES (1,'KOEN');
1 row(s) inserted.

More details here更多细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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