Oracle表结构导出
起因: 要将数据库的表导出,整理成word文档的表格形式。
经过: 真的有很多的表,总不能手动的一个个导吧。
结果: 写个python脚本,然后泡杯茶等它完成。
一、连接数据库
连接数据库要用到cx_Oracle
,先下载。
pip install cx_Oracle
如果报错如下:
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified modu
就需要下载instant-client,解压后将其所有的dll文件,复制到site-packages下面。
具体的连接代码如下:
import cx_Oracle as cx
username = "mydb"
kuName = "127.0.0.1:1521"
password = "u123"
con = cx.connect(username, password, kuName) #建立连接
cursor = con.cursor() #创建游标
二、查询相关数据
查询该用户下的所有表名
import cx_Oracle as cx
cursor.execute("select * from all_tables where owner='" + username.upper() + "' order by TABLE_NAME")
data = cursor.fetchall() # 获取所有表名 [()] 元组列表
tableNameList = []
# 得到表名列表
for x in range(len(data)):
tableNameList.append