- 下载对应系统版本的Oracle Instant Client:https://www.oracle.com/database/technologies/instant-client.html
连接代码
python
import oracledb
def oracle_thick_mode_demo(QHSQL):
try:
# ==============================================
# 1. 初始化Thick模式(必须先执行)
# ==============================================
# 方式A:指定Oracle Instant Client路径(推荐)
oracledb.init_oracle_client(lib_dir=r"C:\ORACLELINK\instantclient_23_0") # Windows示例
# oracledb.init_oracle_client(lib_dir="/opt/oracle/instantclient_19_11") # Linux示例
# 方式B:如果已配置环境变量ORACLE_HOME,可省略lib_dir参数
# oracledb.init_oracle_client()
# ==============================================
# 2. 建立数据库连接
# ==============================================
# 建立连接
connection = oracledb.connect(
user="your_username",
password="your_password",
dsn="localhost:1521/orclpdb1"
)
print("✅ 数据库连接成功!")
# ==============================================
# 3. 执行查询
# ==============================================
with connection.cursor() as cursor:
# 示例1:查询当前用户和数据库版本
cursor.execute("SELECT USER, BANNER FROM V$VERSION WHERE ROWNUM <= 1")
row = cursor.fetchone()
print(f"当前用户: {row[0]}")
print(f"数据库版本: {row[1]}")
# 示例2:查询表格数据(替换为你的表名)
print("\n📋 表格数据:")
cursor.execute(QHSQL) # 限制返回5条数据
# 获取字段名列表
column_names = [desc[0] for desc in cursor.description]
print(" | ".join(column_names)) # 打印字段名
print("-" * (len(" | ".join(column_names)))) # 打印分隔线
# 打印每行数据
for row in cursor:
# 将每行数据转换为字符串并格式化输出
print(" | ".join(str(col) if col is not None else "NULL" for col in row))
# for row in cursor:
# print(row)
except oracledb.Error as e:
print(f"❌ 操作失败: {e}")
finally:
# ==============================================
# 4. 关闭资源
# ==============================================
if 'connection' in locals() and connection:
connection.close()
print("\n🔌 数据库连接已关闭")
if __name__ == "__main__":
QHSQL = """
SELECT
flv.meaning 状态,
'''' || jh.period_name 期间,
to_char(jh.default_effective_date, 'YYYY-MM-DD') 有效日期,
cc.padded_concatenated_segments 帐户,
apps.xla_oa_functions_pkg.get_ccid_description(cc.chart_of_accounts_id,
jl.code_combination_id) 帐户说明,
jh.currency_code 币种,
jl.entered_dr 原币借方金额,
jl.entered_cr 原币贷方金额,
jl.accounted_dr 本币借方金额,
jl.accounted_cr 本币贷方金额,
jh.currency_conversion_rate 汇率,
/*'' 汇率类型,
'' 单位,
'' 数量,*/
jb.name 批名,
jh.name 日记帐名称,
'''' || jh.doc_sequence_value 单据编号,
'' 会计科目排序编号,
jh.description 头说明,
js.user_je_source_name 来源,
jc.user_je_category_name 类别,
to_char(jl.creation_date, 'YYYY-MM-DD HH24:MI:SS') 创建时间,
/*'' 用户名,*/
fu.user_name
|| '_'
|| fu.description 创建人,
jb.approval_status_code 审批状态,
jb.approver_employee_id 审批人,
jb.posted_by 过账人,
jl.je_line_num 行号,
jl.description 行摘要,
jl.attribute6 中台结算凭证号,
jb.status 状态,
to_char(jb.posted_date, 'YYYY-MM-DD HH24:MI:SS') 过账时间,
jh.je_header_id,
jh.reversed_je_header_id,
cc.segment1 公司段,
cc.segment3 科目段,
cc.segment8 往来段 /*,
(nvl(jl.entered_dr, 0) - nvl(jl.entered_cr, 0)) 原币借方减贷方,
(nvl(jl.accounted_dr, 0) - nvl(jl.accounted_cr, 0)) 本位币借方减贷方*/
FROM
gl_je_headers jh
JOIN gl_je_lines jl ON
jh.je_header_id = jl.je_header_id
JOIN gl_je_batches jb ON
jb.je_batch_id = jh.je_batch_id
JOIN gl_code_combinations_kfv cc ON
jl.code_combination_id = cc.code_combination_id
JOIN gl_ledgers ldg ON
ldg.ledger_id = jh.ledger_id
AND cc.chart_of_accounts_id = ldg.chart_of_accounts_id
AND ldg.ledger_category_code = 'PRIMARY'
JOIN gl_je_sources js ON
jh.je_source = js.je_source_name
JOIN gl_je_categories jc ON
jh.je_category = jc.je_category_name
JOIN fnd_flex_values_vl fv ON
fv.flex_value_set_id = 1017489
AND fv.flex_value = cc.segment3
LEFT JOIN fnd_user fu ON
jl.created_by = fu.user_id
JOIN fnd_lookup_values flv ON
jb.status = flv.lookup_code
AND flv.view_application_id = 101
AND flv.language = 'ZHS'
AND flv.lookup_type = 'BATCH_STATUS'
WHERE
1 = 1
AND jh.period_name = '2025-13'
-- AND ROWNUM <= 200
"""
oracle_thick_mode_demo(QHSQL)