I'm trying to create an Oracle table from a list of attributes with python. Sadly, I have multiple attributes with the same name thus I can't add them to the table. Also I don't want my program to stop because of that. Now I'm trying with this solution:
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)
I've pasted the long query code from this answer. The table is created with the first attribute as intended but as I would start to add more columns I get:
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
What am I missing?