Get database's schema, which is a dict with table name as key and list of column names as value :param db: database path :return: schema dict
(db)
| 77 | |
| 78 | |
| 79 | def get_schema(db): |
| 80 | """ |
| 81 | Get database's schema, which is a dict with table name as key |
| 82 | and list of column names as value |
| 83 | :param db: database path |
| 84 | :return: schema dict |
| 85 | """ |
| 86 | |
| 87 | schema = {} |
| 88 | conn = sqlite3.connect(db) |
| 89 | cursor = conn.cursor() |
| 90 | |
| 91 | # fetch table names |
| 92 | cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") |
| 93 | tables = [str(table[0].lower()) for table in cursor.fetchall()] |
| 94 | |
| 95 | # fetch table info |
| 96 | for table in tables: |
| 97 | cursor.execute("PRAGMA table_info({})".format(table)) |
| 98 | schema[table] = [str(col[1].lower()) for col in cursor.fetchall()] |
| 99 | |
| 100 | return schema |
| 101 | |
| 102 | |
| 103 | def get_schema_from_json(fpath): |