Beispiel #1
0
    def create(self, table_name):
        # 输入表字段
        print('请输入表数据 json格式 如 {"name":"张三"}\n等待输入:\n')
        data = self.get_data()

        if not isinstance(data, dict):
            raise Exception("表数据格式不正确")

        # 拼接表结构
        sql = """
            CREATE TABLE `{db}`.`{table_name}` (
                `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id 自动递增',
                {other_key}
                `gtime` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '抓取时间',
                PRIMARY KEY (`id`),
                {unique}
            ) COMMENT='';
        """

        print("请设置注释 回车跳过")
        other_key = ""
        for key, value in data.items():
            key = key2underline(key)
            key_type = self.get_key_type(value)

            comment = input("%s : %s  -> comment:" % (key, key_type))

            other_key += "`{key}` {key_type} COMMENT '{comment}',\n                ".format(
                key=key, key_type=key_type, comment=comment)

        print("\n")

        while True:
            is_need_batch_date = input("是否添加batch_date 字段 (y/n):")
            if is_need_batch_date == "y":
                other_key += "`{key}` {key_type} COMMENT '{comment}',\n                ".format(
                    key="batch_date", key_type="date", comment="批次时间")
                break
            elif is_need_batch_date == "n":
                break

        print("\n")

        while True:
            unique = input("请设置唯一索引, 多个逗号间隔\n等待输入:\n").replace(",", ",")
            if unique:
                break
        unique = "UNIQUE `idx` USING BTREE (`%s`) comment ''" % "`,`".join(
            unique.split(","))

        sql = sql.format(
            db=setting.MYSQL_DB,
            table_name=table_name,
            other_key=other_key,
            unique=unique,
        )
        print(sql)
        self._db.execute(sql)
        print("\n%s 创建成功" % table_name)
Beispiel #2
0
    def name_underline(self):
        if not self.__name_underline__:
            self.__name_underline__ = tools.key2underline(self.item_name)

        return self.__name_underline__
Beispiel #3
0
    def create(self, table_name):
        # 输入表字段
        data = self.get_data()

        if not isinstance(data, dict):
            raise Exception("表数据格式不正确")

        # 拼接表结构
        sql = """
            CREATE TABLE `{db}`.`{table_name}` (
                `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id主键',
                {other_key}
                `crawl_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '采集时间',
                {unique}
                PRIMARY KEY (`id`)
            ) COMMENT='';
        """

        # print("请设置注释 回车跳过")
        other_key = ""
        for key, value in data.items():
            key = key2underline(key)
            comment = ""
            if key == "id":
                key = "data_id"
                comment = "原始数据id"

            key_type = self.get_key_type(value)

            # comment = input("%s : %s  -> comment:" % (key, key_type))

            other_key += (
                "`{key}` {key_type} COMMENT '{comment}',\n                ".
                format(key=key, key_type=key_type, comment=comment))

        print("\n")

        while True:
            yes = input("是否添加批次字段 batch_date(y/n):")
            if yes == "y":
                other_key += (
                    "`{key}` {key_type} COMMENT '{comment}',\n                "
                    .format(key="batch_date", key_type="date", comment="批次时间"))
                break
            elif yes == "n":
                break

        print("\n")

        while True:
            yes = input("是否设置唯一索引(y/n):")
            if yes == "y":
                unique = input("请设置唯一索引, 多个逗号间隔\n等待输入:\n").replace(",", ",")
                if unique:
                    unique = "UNIQUE `idx` USING BTREE (`%s`) comment ''," % "`,`".join(
                        unique.split(","))
                    break
            elif yes == "n":
                unique = ""
                break

        sql = sql.format(
            db=setting.MYSQL_DB,
            table_name=table_name,
            other_key=other_key.strip(),
            unique=unique,
        )
        print(sql)

        if self._db.execute(sql):
            print("\n%s 创建成功" % table_name)
            print("注意手动检查下字段类型,确保无误!!!")
        else:
            print("\n%s 创建失败" % table_name)