python编写脚本,删除固定用户下的所有表
生活随笔
收集整理的這篇文章主要介紹了
python编写脚本,删除固定用户下的所有表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
腳本如下:
[oracle@ycr python]$ more t_del.py?
#/usr/bin/python
#coding:utf8
import sys
import cx_Oracle
i=0
conn=cx_Oracle.connect('%s/%s@%s' % (sys.argv[1],sys.argv[2],sys.argv[3]))
cursor=conn.cursor()
cursor.execute('select table_name from user_tables')
rows=cursor.fetchall()
for row in rows:
? ? cursor.execute('drop table %s cascade constraints purge' % row)?
? ? i+=1
cursor.close()
conn.close()
print 'Drop table complete! %d tables droped' % i
測試
創建測試表,使用test用戶執行如下腳本:
create table t1 as select * from user_tables;
create table t2 as select * from user_tablespaces;
create table t3 as select * from user_objects;
執行程序:
python t_del.py test oracle YCR2
此小程序有三個參數,test為要刪除表的用戶名,oracle為密碼,YCR2為連接字符串。
執行結果如下:
python t_del.py test oracle YCR2
Drop table complete! 3 tables droped
------------------------------------------------------------------------------
本腳本寫來練習用,實際意義不是很大,昨天開發同時讓幫忙spool出來一個刪除表的腳本,自動化要求較高,所以寫了個小程序。
不過執行此程序需要安裝cx_Oracle模塊,相對繁瑣。
Clark
2016.08.03
[oracle@ycr python]$ more t_del.py?
#/usr/bin/python
#coding:utf8
import sys
import cx_Oracle
i=0
conn=cx_Oracle.connect('%s/%s@%s' % (sys.argv[1],sys.argv[2],sys.argv[3]))
cursor=conn.cursor()
cursor.execute('select table_name from user_tables')
rows=cursor.fetchall()
for row in rows:
? ? cursor.execute('drop table %s cascade constraints purge' % row)?
? ? i+=1
cursor.close()
conn.close()
print 'Drop table complete! %d tables droped' % i
測試
創建測試表,使用test用戶執行如下腳本:
create table t1 as select * from user_tables;
create table t2 as select * from user_tablespaces;
create table t3 as select * from user_objects;
執行程序:
python t_del.py test oracle YCR2
此小程序有三個參數,test為要刪除表的用戶名,oracle為密碼,YCR2為連接字符串。
執行結果如下:
python t_del.py test oracle YCR2
Drop table complete! 3 tables droped
------------------------------------------------------------------------------
本腳本寫來練習用,實際意義不是很大,昨天開發同時讓幫忙spool出來一個刪除表的腳本,自動化要求較高,所以寫了個小程序。
不過執行此程序需要安裝cx_Oracle模塊,相對繁瑣。
Clark
2016.08.03
轉載于:https://www.cnblogs.com/Clark-cloud-database/p/7813563.html
總結
以上是生活随笔為你收集整理的python编写脚本,删除固定用户下的所有表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: runtest的选项应用
- 下一篇: Python学习之路:函数介绍