sqlite3から全データの抽出って、どうやってやるんだろう?
とりあえず、力技でプログラムを書いてみた。
import sqlite3 import json conn = sqlite3.connect('./db20201002.sqlite3') cur = conn.cursor() actionType = 'allData' strAllData = '' cur.execute('select name from sqlite_master where type="table";') tables = cur.fetchall() strAllData = '{\n' for table in tables : # テーブル名 print(table[0]) strAllData += '\t"'+table[0]+'" : [\n' cur.execute('select * from '+ table[0]) tableContent = cur.fetchall() desc = cur.description for rec in tableContent: strAllData += '\t\t{\n' colNum = 0 for val in rec: strAllData += '\t\t\t"'+str(desc[colNum][0]) + '" : "' + str(val) + '", \n' colNum = colNum + 1 strAllData += '\t\t},\n' strAllData += '\t],\n' strAllData += '}\n' with open('./allData.json', mode='w',encoding='UTF-8') as f: f.write(strAllData) jsAllData = json.dumps(strAllData) cur.close() conn.close()
@@@
python2.5以降は、特に何かインストールすることなく使うことができるみたいですね。
python — sqlite3をPythonにインストールするにはどうすればよいですか?
table一覧の抽出方法
SQLite3でテーブル一覧を取得する | 俺的備忘録 〜なんかいろいろ〜