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)
| 106 | |
| 107 | |
| 108 | def get_schema(db): |
| 109 | """ |
| 110 | Get database's schema, which is a dict with table name as key |
| 111 | and list of column names as value |
| 112 | :param db: database path |
| 113 | :return: schema dict |
| 114 | """ |
| 115 | |
| 116 | schema = {} |
| 117 | conn = sqlite3.connect(db) |
| 118 | cursor = conn.cursor() |
| 119 | |
| 120 | # fetch table names |
| 121 | cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") |
| 122 | tables = [str(table[0].lower()) for table in cursor.fetchall()] |
| 123 | |
| 124 | # fetch table info |
| 125 | for table in tables: |
| 126 | cursor.execute("PRAGMA table_info({})".format(table)) |
| 127 | schema[table] = [str(col[1].lower()) for col in cursor.fetchall()] |
| 128 | |
| 129 | return schema |
| 130 | |
| 131 | |
| 132 | def get_schema_from_json(fpath): |