| 72 | |
| 73 | # 获取表结构 |
| 74 | def get_table_info(db_obj, table): |
| 75 | field_info = {} |
| 76 | wheres = [] |
| 77 | field_keys = [] |
| 78 | tb_list = db_obj.query("PRAGMA table_info({});".format(table)) |
| 79 | if not isinstance(tb_list, list): |
| 80 | if 'unable to open database file' in tb_list: |
| 81 | return None |
| 82 | print(table, tb_list) |
| 83 | print_x('{} 表结构读取失败,可能原因表已损坏'.format(table)) |
| 84 | return None |
| 85 | |
| 86 | for tb in tb_list: |
| 87 | field_info[tb[1]] = tb |
| 88 | field_keys.append('`{}`'.format(tb[1])) |
| 89 | wheres.append('`{}`=?'.format(tb[1])) |
| 90 | |
| 91 | res = db_obj.query("SELECT * FROM sqlite_master WHERE type='table' AND name='{}';".format(table)) |
| 92 | if not isinstance(res, list): |
| 93 | return None |
| 94 | |
| 95 | if len(res) == 0: |
| 96 | return None |
| 97 | data = {} |
| 98 | data['field'] = field_info |
| 99 | data['sql'] = res[0][4] |
| 100 | data['field_keys'] = field_keys |
| 101 | data['wheres'] = wheres |
| 102 | |
| 103 | return data |
| 104 | |
| 105 | |
| 106 | # 检查表是否存在 |