diff options
author | Keuin <[email protected]> | 2022-03-27 18:48:23 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-03-27 18:48:23 +0800 |
commit | 8bcf798d2d78b44fb6ab18cb67af444f896d2414 (patch) | |
tree | e1cc3302e89d8df5023d9e9156aae7e6b3754482 | |
parent | aae1f386456a605c12263e2ba5f09dc36bb180b3 (diff) |
Add old database conversion script.
-rw-r--r-- | database_convert.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/database_convert.py b/database_convert.py new file mode 100644 index 0000000..356f84c --- /dev/null +++ b/database_convert.py @@ -0,0 +1,26 @@ +import json +import sqlite3 + + +# noinspection SqlNoDataSourceInspection +def convert(db_file: str = 'kimikuri.db', json_file: str = 'users.json'): + db = sqlite3.connect(db_file) + with db, open(json_file, 'r', encoding='utf-8') as f: + j = json.load(f) + cur = db.cursor() + cur.execute('''CREATE TABLE IF NOT EXISTS "user" ( + "id" INTEGER NOT NULL CHECK(id >= 0) UNIQUE, + "name" TEXT, + "token" TEXT NOT NULL UNIQUE, + "chat_id" INTEGER NOT NULL CHECK(chat_id >= 0) UNIQUE, + PRIMARY KEY("id","token","chat_id") + )''') + cur.executemany( + 'INSERT INTO user (id, name, token, chat_id) VALUES (?,?,?,?)', + [(x['user_id'], None, x['token'], x['chat_id']) for x in j] + ) + db.close() + + +if __name__ == '__main__': + convert() |