[英]Python code to list the active database from IBM UDB DB2
有人可以幫助我通過 IBM DB2 的 python 代碼獲取活動數據庫列表
有不止一種方法可以做到這一點,但這里有一個使用監控查詢的示例。 您的用戶 ID 需要適當的授權才能運行查詢,有關詳細信息,請參閱文檔。
#!/usr/bin/python3
#
# the shell session that runs this script needs
# the correct environment variables to be already set
# (usually by dotting in the correct db2profile for the Db2 instance)
# otherwise ibm_db will fail to import citing load failure
# on a db2 library.
#
import sys
import ibm_db
db_user=''
db_name='sample'
db_pwd=''
try:
ibm_db_conn = ibm_db.connect(db_name, db_user, db_pwd)
except:
print('Failed to connect to database' + db_name)
print(ibm_db.conn_errormsg())
sys.exit(1)
try:
action='Query snap_get_db'
active_databases_query="SELECT db_name FROM TABLE(snap_get_db(NULL))"
stmt_handle = ibm_db.exec_immediate(ibm_db_conn, active_databases_query)
if stmt_handle :
action = 'Fetching resultSet returned by query'
row = ibm_db.fetch_tuple(stmt_handle)
while (row ):
for i in row:
print(i)
row = ibm_db.fetch_tuple(stmt_handle)
except:
print('Failed on ' + action)
print(ibm_db.errormsg())
sys.exit(1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.