• python读取excel表格生成sql语句 第一版


    由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦  作为程序猿当然要想办法解决,用Python写一个程序解决

    需要用到 xlrd linux下 sudo pip install xlrd

    主要是适用于db2数据库

    excel 表结构 其中 number是不正确的字段类型 不知道同事为啥这么设置。这里程序里有纠错,这个程序就是将sql语句拼好。

    __author__ = 'zhanglei'
    # coding:utf-8
    
    import xlrd
    import re
    
    
    data = xlrd.open_workbook("1.xlsx")
    table = data.sheets()[0]
    
    temp = table.row_values(0)[0]
    
    tableName = re.findall("[A-Z].*w+", temp)[0]
    
    nrows = table.nrows
    #print nrows
    sql = "create table " + tableName + "( 
    "
    for rownum in range(2, nrows):
        row = table.row_values(rownum)
    
        if row and rownum != (nrows - 1):
    
            if row[1] == "ID":
                temp = float(row[3])
                sql += row[1] + " " + row[2] + "(" + str(int(temp)) + ") " + "PRIMARY KEY,
    "
            else:
                sql += row[1] + " "
                if re.search("DECI.*", row[2]):
                    sql += " " + row[2]
                elif row[2] == "NUMBER" and row[3] == 8:
                    sql += " int "
                elif row[2] == "NUMBER" and row[3] == 1:
                    sql += " smallint "
                elif row[2] == "NUMBER" and row[3] > 10:
                    sql += "bigint"
                elif row[2] == "DATETIME":
                    sql += " timestamp "
                elif row[2] == "DATE":
                    sql += " date "
                else:
                    temp = float(row[3])
                    sql += " " + row[2] + "(" + str(int(temp)) + ") "
    
                if row[4] == "Y" and row[5] == "Y":
                    sql += " NOT NULL UNIQUE,
    "
    
                elif row[4] == "Y" and row[5] != "Y":
                    sql += " NOT NULL,
    "
                elif row[4] != "Y" and row[5] != "Y":
                    sql += ",
    "
        else:
            sql += row[1] + " "
    
            if re.search("DECI.*", row[2]):
                sql += " " + row[2]
            else:
                temp = float(row[3])
                sql += " " + row[2] + "(" + str(int(temp)) + ") "
    
            if row[4] == "Y" and row[5] == "Y":
                sql += " NOT NULL UNIQUE,
    "
    
            elif row[4] == "Y" and row[5] != "Y":
                sql += " NOT NULL,
    "
            elif row[4] != "Y" and row[5] != "Y":
                sql += " 
    )"
    
    print sql
     
    create table BH_Business( 
    ID VARCHAR(64) PRIMARY KEY,
    BUSI_SERIAL_NO  VARCHAR(50)  NOT NULL UNIQUE,
    BUSI_CODE  VARCHAR(10)  NOT NULL,
    BRANCH_CODE  VARCHAR(10)  NOT NULL,
    TELLER_CODE  VARCHAR(10)  NOT NULL,
    AMT  DECIMAL(14,2) NOT NULL,
    CURRENCY  VARCHAR(6)  NOT NULL,
    CUSTOM_LVL  NUMBER(1)  NOT NULL,
    STATE  VARCHAR(2)  NOT NULL,
    REMARKS  VARCHAR(200) ,
    WEIGHT_VALUE  NUMBER(8)  NOT NULL,
    TMP_WEIGHT_VALUE  NUMBER(8)  NOT NULL,
    URGENT_FLAG  NUMBER(1)  NOT NULL,
    ACCP_TIME  timestamp  NOT NULL,
    CLOSE_TIME  timestamp  NOT NULL,
    WORK_FLOW_ID  VARCHAR(200) ,
    TMP_UNDO_FLAG  NUMBER(1)  NOT NULL,
    SYS_ID  VARCHAR(6)  NOT NULL,
    MEDIUM  VARCHAR(8)  NOT NULL,
    CRT_TELLER_ID  VARCHAR(50)  NOT NULL,
    CRT_TIME  timestamp  NOT NULL,
    CRT_IP  VARCHAR(50)  NOT NULL,
    UPD_TELLER_ID  VARCHAR(50) ,
    UPD_TIME  timestamp ,
    UPD_IP  VARCHAR(50)  
    )
  • 相关阅读:
    《分布式之数据库缓存双写一致性方案解析》
    淘系工程师讲解的使用Spring特性优雅书写业务代码
    简述BIO/NIO/AIO前世今生
    经典面试题:分布式缓存热点KEY问题如何解决有赞方案
    软件中的文本本地化
    Java Instant\Date\LocalDateTime\Calendar\ZonedDateTime转化
    java反序列化漏洞专项
    web打印样式预览调试技巧
    公钥私钥 与 http/https
    docker安装mysql
  • 原文地址:https://www.cnblogs.com/or2-/p/3926048.html
Copyright © 2020-2023  润新知