我正在尝试使用python访问和更新Oracle数据库。以下是我的代码:
import cx_Oracle
import pandas as pd
import datetime
import numpy
import math
#import pandasql
conn_str = 'sahil/sahil@52.20.141.126:1521/xe'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
def update_output_table(customer_id_list,column_name,column_vlaue_list) :
num_rows_to_add = len(customer_id_list)
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
for i in range(0,num_rows_to_add,1) :
c.execute("""UPDATE output SET """+column_name+""" = %s WHERE customer_id = %s""" %(column_vlaue_list[i],customer_id_list[i]))
print "Completed updating " + column_name
conn.commit()
total_transaction_df = pd.read_sql("""select distinct b.customer_id,count(a.transaction_id) as total_transaction from transaction_fact a,customer_dim b where a.customer_id = b.customer_id group by b.customer_id""",conn)
# Update this details to the output table
update_output_table(list(total_transaction_df['CUSTOMER_ID']),'TOTAL_TRANSACTION',list(total_transaction_df['TOTAL_TRANSACTION']))
english_movies_df = pd.read_sql("""select b.customer_id, count(a.product_id) as "ENGLISH_MOVIES" from transaction_fact a inner join customer_dim b on a.customer_id = b.customer_id where a.product_id like 'E%' group by b.customer_id""",conn)
# Update this details to the output table
update_output_table(list(english_movies_df['CUSTOMER_ID']),'ENGLISH_MOVIES',list(english_movies_df['ENGLISH_MOVIES']))
hindi_movies_df = pd.read_sql("""select b.customer_id, count(a.product_id) as "HINDI_MOVIES" from transaction_fact a inner join customer_dim b on a.customer_id = b.customer_id where a.product_id like 'H%' group by b.customer_id""",conn)
# Update this details to the output table
update_output_table(list(hindi_movies_df['CUSTOMER_ID']),'HINDI_MOVIES',list(hindi_movies_df['HINDI_MOVIES']))
most_popular_genre_df = pd.read_sql("""select x.customer_id, x.genre as "MOST_POPULAR_GENRE",x.count1 from (select b.customer_id,c.genre,count(a.transaction_id) as count1, RANK() OVER (PARTITION BY b.customer_id order by count(a.transaction_id) desc,c.genre) as Rank1 from transaction_fact a inner join customer_dim b on a.customer_id = b.customer_id inner join product_dim c on a.product_id = c.product_id group by b.customer_id, c.genre)x where x.Rank1 = 1""",conn)
# Update this details to the output table
update_output_table(list(most_popular_genre_df['CUSTOMER_ID']),'MOST_POPULAR_GENRE',list(most_popular_genre_df['MOST_POPULAR_GENRE']))
conn.close()
整个脚本正在执行,但最后一次调用update_output_table
的地方除外ORA-00904 : 'UNCONVENTIONAL' invalid identifier
。
我已经传递了正确的列名。以下是我创建output
表的代码:
create table output
(
customer_id integer,
total_transaction integer,
english_movies integer,
hindi_movies integer,
most_popular_genre varchar2(30),
last_transaction_date date,
overall_score integer
)
UNCONVENTIONAL
是我的事实表产品类别的第一个条目。是选择UNCONVENTIONAL
作为我的列名而不是MOST_POPULAR_GENRE
吗?如果是,那怎么回事?
注意:我是新手,所以如果这是一个愚蠢的怀疑,请光顾。
答案 0 :(得分:0)
由于您的代码中没有提及字段UNCONVENTIONAL
,因此很难指出问题所在。但错误信息告诉了很多事情。我鼓励您理解错误消息,以便将来帮助您。
Oracle / PLSQL:ORA-00904错误消息
<强>描述强>
如果遇到ORA-00904
错误,将显示以下错误消息:
ORA-00904: invalid identifier
<强>原因强>
您尝试执行包含无效列名或缺少列名的SQL语句。当您在SELECT
语句中引用无效别名时,通常会发生这种情况。
解决强>
重写SQL以包含有效的列名。要成为有效的列名,必须满足以下条件:
$, _, and #
。如果列名使用任何其他字符,则必须用双引号括起来。让我们看一个如何解决ORA-00904错误的例子。
SQL> SELECT contact_id AS "c_id", last_name, first_name
2 FROM contacts
3 ORDER BY "cid";
ORDER BY "cid"
*
ERROR at line 3:
ORA-00904: "cid": invalid identifie
此错误是通过对列进行别名来创建的,但稍后会对别名进行错误输入。在这个例子中,我们为contact_id创建了名为“c_id”的别名,但后来在ORDER BY子句中将其称为“cid”。
要解决此错误,我们可以修改SELECT语句,以便在ORDER BY子句中使用正确的别名,如下所示:
SQL> SELECT contact_id AS "c_id", last_name, first_name
2 FROM contacts
3 ORDER BY "c_id";
10 rows selected