(self, table_name)
| 69 | return tools.get_json(text) |
| 70 | |
| 71 | def create(self, table_name): |
| 72 | # 输入表字段 |
| 73 | data = self.get_data() |
| 74 | |
| 75 | if not isinstance(data, dict): |
| 76 | raise Exception("表数据格式不正确") |
| 77 | |
| 78 | # 拼接表结构 |
| 79 | sql = """ |
| 80 | CREATE TABLE `{db}`.`{table_name}` ( |
| 81 | `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id主键', |
| 82 | {other_key} |
| 83 | `crawl_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '采集时间', |
| 84 | {unique} |
| 85 | PRIMARY KEY (`id`) |
| 86 | ) COMMENT=''; |
| 87 | """ |
| 88 | |
| 89 | # print("请设置注释 回车跳过") |
| 90 | other_key = "" |
| 91 | for key, value in data.items(): |
| 92 | key = key2underline(key) |
| 93 | comment = "" |
| 94 | if key == "id": |
| 95 | key = "data_id" |
| 96 | comment = "原始数据id" |
| 97 | |
| 98 | key_type = self.get_key_type(value) |
| 99 | |
| 100 | # comment = input("%s : %s -> comment:" % (key, key_type)) |
| 101 | |
| 102 | other_key += ( |
| 103 | "`{key}` {key_type} COMMENT '{comment}',\n ".format( |
| 104 | key=key, key_type=key_type, comment=comment |
| 105 | ) |
| 106 | ) |
| 107 | |
| 108 | print("\n") |
| 109 | |
| 110 | while True: |
| 111 | yes = input("是否添加批次字段 batch_date(y/n):") |
| 112 | if yes == "y": |
| 113 | other_key += ( |
| 114 | "`{key}` {key_type} COMMENT '{comment}',\n ".format( |
| 115 | key="batch_date", key_type="date", comment="批次时间" |
| 116 | ) |
| 117 | ) |
| 118 | break |
| 119 | elif yes == "n": |
| 120 | break |
| 121 | |
| 122 | print("\n") |
| 123 | |
| 124 | while True: |
| 125 | yes = input("是否设置唯一索引(y/n):") |
| 126 | if yes == "y": |
| 127 | unique = input("请设置唯一索引, 多个逗号间隔\n等待输入:\n").replace(",", ",") |
| 128 | if unique: |
nothing calls this directly
no test coverage detected