python中读取数据库,执行报错。原因:红色部分不能加分号; (加了 ;号在idea 数据库链接下可以执行)。具体见下面代码:
import cx_Oracle
username = '999'
password = '9999'
# 设置数据库连接信息
dsn = cx_Oracle.makedsn('9.9.0.99', '9', 'hh') # 替换为你的数据库主机名、端口和服务名
try:
connection = cx_Oracle.connect(user=username, password=password, dsn=dsn)
print("Connected to Oracle Database successfully!")
# 获取游标
cursor = connection.cursor()
# 使用SQL查询获取表SYS_MENU的所有列信息
sql_query = """
SELECT column_name, data_type, data_length, nullable
FROM all_tab_columns
WHERE table_name = 'SYS_MENU'
AND owner = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')
"""
cursor.execute(sql_query)
# 打印字段信息
print("Fields of the table SYS_MENU:")
for row in cursor:
print(
f"Column Name: {row[0]}, Data Type: {row[1]}, Data Length: {row[2]}, Nullable: {'Yes' if row[3] == 'Y' else 'No'}")
except cx_Oracle.Error as error:
print(f"Error occurred: {error}")
finally:
# 关闭连接和游标
if cursor:
cursor.close()
if connection:
connection.close()
print("Connection closed.")

8689

被折叠的 条评论
为什么被折叠?



