我正在尝试使用 python 从属性列表中创建一个 Oracle 表。可悲的是,我有多个同名的属性,因此我无法将它们添加到表中。我也不希望我的程序因此而停止。现在我正在尝试这个解决方案:
connection = cx_Oracle.connect('user/password')
cursor = connection.cursor()
if not tableExists(connection, 'TableName'):
first_column_name = next(iter(attributes), None)
query_table = 'CREATE TABLE TableName ("{}" VARCHAR2(255))'.format(first_column_name)
cursor.execute(query_table)
for attribute in attributes[1:]:
query_column= '''
DECLARE
v_column_exists number := 0;
BEGIN
Select count(*) into v_column_exists
from user_tab_cols
where upper(column_name) = "{}"
and upper(table_name) = 'TableName';
if (v_column_exists = 0) then
execute immediate 'alter table TableName add ("{}" VARCHAR2(255)))';
end if;
end;
'''.format(attribute, attribute)
cursor.execute(query_column)
我已经粘贴了这个答案的长查询代码。该表是按预期使用第一个属性创建的,但是当我开始添加更多列时,我得到:
Traceback (most recent call last):
File "main.py", line 52, in <module>
cursor.execute(query_column)
cx_Oracle.DatabaseError: ORA-06550: line 7, column 41:
PL/SQL: ORA-00904: "Order count [A221]": invalid identifier
ORA-06550: line 5, column 9:
PL/SQL: SQL Statement ignored
我错过了什么?