【问题标题】:Python : Accessing oracle database and updating itPython:访问oracle数据库并更新它
【发布时间】:2016-12-12 17:22:08
【问题描述】:

我正在访问 oracle 数据库并尝试使用 python 更新它。以下是我的代码:

import cx_Oracle
import pandas as pd
import datetime
import numpy
import math
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]))

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']))

conn.close()

我的程序正在完全执行,但我没有看到我的数据库表得到更新。有人可以建议我哪里出错了吗?

注意:我是新手。很抱歉提出愚蠢的疑问。提前致谢。

【问题讨论】:

    标签: python oracle


    【解决方案1】:

    你在conn.close()之前缺少conn.commit()

    Here 你会找到一些你为什么需要它的信息。如果没有提交,您的代码正在更新,然后在关闭连接时,所有未提交的更改都将回滚,因此您在 DB 中看不到任何更改。
    您也可以设置cx_Oracle.Connection.autocommit = 1,但不建议这样做,因为您将失去对transactions 的控制。

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多