ITPub博客

首页 > 数据库 > Oracle > python编写脚本,删除固定用户下的所有表

python编写脚本,删除固定用户下的所有表

原创 Oracle 作者:kisslfcr 时间:2016-08-03 09:04:46 0 删除 编辑
脚本如下:

[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


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29135257/viewspace-2122884/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: linux配置sudo
请登录后发表评论 登录
全部评论

注册时间:2015-01-12

  • 博文量
    246
  • 访问量
    426060