在Python查询中将Oracle表名称作为变量传递

问题描述 投票:0回答:1

如何在 Python 查询中将 Oracle 表名作为参数传递以执行?

conn_str = u'user/password@host:port/service'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
string table_name = "abctable"
1.c.execute(u'select your_col_1, your_col_2 from'+table_name)   
2.c.execute(u'select your_col_1, your_col_2 from &table_name')
3.c.execute(u'select your_col_1, your_col_2 from :table_name')

'''which one is right for python

for row in c:
    print row[0], "-", row[1]
conn.close()

请向我提供有关如何使用 Oracle 在 Python 查询中传递表名的想法?

python oracle
1个回答
0
投票

您可以将值作为绑定值传递;您不能将标识符(例如表名或列名)作为绑定变量传递。

如果您想包含标识符,那么您需要将其包含到字符串中(而不是作为绑定变量):

c.execute("select your_col_1, your_col_2 from " + table_name)

注意

from
和表名称之间的尾随空格。

c.execute(
  "select your_col_1, your_col_2 from {table_name}".format(table_name=table_name),
)

c.execute(f"select your_col_1, your_col_2 from {table_name}")
© www.soinside.com 2019 - 2024. All rights reserved.