首页 > 数据库 >python读取ddl生成sql建表语句

python读取ddl生成sql建表语句

时间:2023-03-24 09:24:29浏览次数:41  
标签:建表 name python ddl df sql table type col

# 导入需要的库
import pandas as pd
import os

def read_ddl_create_tab_sql(file_path:str,table_name:str):
    df = pd.read_csv(file_path,sep='\|\@\|',index_col=0,header=None,encoding='utf-8',engine='python')

    inds,cols = df.shape
    # print(df.head())

    df = df.fillna(0)

    print(df.head())

    # 循环生成Oracle建表语句

 
    table_definition = ''
    col_commit = ''
    for ind in range(inds):
        col_name = df.iloc[ind,0]
        col_type = df.iloc[ind,1]
        col_length_0  = df.iloc[ind,2]
        col_length_1 = df.iloc[ind, 3]
        not_null = df.iloc[ind,4]
        is_pk = df.iloc[ind,5]
        col_comments = df.iloc[ind,6]

        if col_type == 'DATE':
            table_definition += col_name + " " + col_type + (' NOT NULL' if is_pk == 'Y' else '')  + ('' if ind == inds-1 else ",\n")

        if col_type == "VARCHAR2":
            table_definition += col_name + " " + col_type + '({})'.format(int(col_length_0)) + (' NOT NULL' if is_pk == 'Y' else '') + \
                                ('' if ind == inds - 1 else ",\n")

        if col_type == 'NUMBER':
            col_type_num = ''
            if col_length_0 >0 and col_length_1 >0:
                col_type_num = '({},{})'.format(int(col_length_0), int(col_length_1))
            elif col_length_0 > 0 and col_length_1 == 0:
                col_type_num = '({})'.format(col_length_0)
            else:
                col_type_num = ''

            table_definition += col_name + " " + col_type + \
                                col_type_num + \
                                (' NOT NULL ' if is_pk == 'Y' else '') + \
                                ('' if ind == inds-1 else ",\n")
        col_commit = col_commit +  "comment on column "+table_name+"." + col_name+ " is '{}'".format(col_comments) + ";\n"
    # 输出建表语句
    drop_tab_sql = 'DROP TABLE {} PURGE;'.format(table_name)
    create_sql_str = drop_tab_sql + '\n' + 'CREATE TABLE ' + table_name + ' (' + table_definition + ') TABLESPACE TSODSDAT;\n' + col_commit
    # print(create_sql_str)


    with open("./create_tab_sql/{}.sql".format(table_name), 'w',encoding='utf-8') as f:
        f.write(str_s)

    return create_sql_str


def del_file(dir):
    for f in os.listdir(dir):
        os.remove(os.path.join(dir, f))

# 读取数据文件
file_path = './ddl_data/'

del_file("./create_tab_sql/")
str_s = ''
for etm in os.listdir(file_path):
    if etm.split('.')[1] == 'ddl' and '_TX_' in etm:
        table_name,name_suffix = etm.split("_D_")
        str_s = str_s + read_ddl_create_tab_sql(file_path+etm,table_name) + '\n\n'


# os.remove("./create_tab_sql/create_table_sql.sql")
with open("./create_tab_sql/create_table_sql.sql",'w',encoding='utf-8') as f:
    f.write(str_s)

  

标签:建表,name,python,ddl,df,sql,table,type,col
From: https://www.cnblogs.com/wuzaipei/p/17250229.html

相关文章

  • python对整个目录下面的所有文件进行处理的代码
    对整个目录里面的所有文件进行处理.实用场景:1)在整个目录下的文件中,哪些文件中有某个关键字出现2)备份某个目录下面的文件,比如:按照建立日期或者文件名按照月份进行......
  • 使用 Python 探索 感知机 算法
    动动发财的小手,点个赞吧!从理论到实践,我们将从简要的理论介绍开始研究感知机(器)学习方法,然后实现。在这篇博文的最后,您将能够了解何时以及如何使用这种机器学习算法,清楚......
  • Python多进程代码调试工具
    https://github.com/Lightning-AI/forked-pdbimportsysimportpdbclassForkedPdb(pdb.Pdb):"""PDBSubclassfordebuggingmulti-processedcodeSug......
  • python内置函数2
    sum()sorted()reversed()all()any()enumerate()zip()map()filter()......
  • python内置函数1
    abs()divmod()pow()len()ord()chr()id()min()max()......
  • 给程序加个进度条吧!1行Python代码,快速搞定~
    大家好,这里是程序员晚枫。你在写代码的过程中,有没有遇到过以下问题?已经写好的程序,想看看程序执行的进度?在写代码批量处理文件的时候,如何显示现在处理到第几个文件了......
  • Python对象的比较、拷贝
    '==' VS 'is'等于(==)和is是Python中对象比较常用的两种方式。简单来说,'=='操作符比较对象之间的值是否相等,而'is'操作符比较的是对象的身份标识是否相等,即它们是否是......
  • python入门
    数字类型上数字类型下布尔类型print()--BIFinput()--BIFint()--BIFdecimal--十进制定点和浮点运算range()--BIF......
  • Python基础
    列表方法用法案例字符串方法字典方法用法案例集合方法1方法2用法案例文件对象方法......
  • openSUSE下修改python3为默认python
    前言在大多数基于Ubuntu和Debian的Linux发行版中,python命令通常指向Python2解释器,而python3命令则指向Python3解释器。这可能会导致使用Python3的脚本和......