-
From #4001 |
Beta Was this translation helpful? Give feedback.
Answered by
anki-code
Mar 2, 2021
Replies: 1 comment 1 reply
-
My solution for xonsh 0.9.24 on Ubuntu:
import json, sqlite3
conn = sqlite3.connect('xonsh-history.sqlite')
cur = conn.cursor()
sql = 'INSERT INTO xonsh_history ({}) VALUES ({});'
i = 0
for f in g`*.json`:
sessionid = f.replace('xonsh-','').replace('.json', '')
try:
j = json.load(open(f))
except:
print(f'Skip {f}: json error')
continue
for c in j['data']['cmds']:
row = {
'inp': c['inp'],
'rtn': c['rtn'],
'tsb': c['ts'][0],
'tse': c['ts'][1],
'sessionid': sessionid
}
columns = ', '.join(row)
values = ', '.join(['?']*len(row))
cur.execute(sql.format(columns, values), tuple(row.values()))
i += 1
conn.commit()
print(f'Inserted {i} commands.')
PS: also the code around history could be found in #1614 (Optionally) Drop duplicates from sqlite historyDrop duplicates from sqlite history (xonsh 0.9.24): -- Get the structure from `sqlite_web @$(history file)`
CREATE TABLE xonsh_history_new (
inp TEXT,
rtn INTEGER,
tsb REAL,
tse REAL,
sessionid TEXT,
out TEXT,
info TEXT,
frequency INTEGER default 1
);
INSERT INTO xonsh_history_new
SELECT inp, min(rtn), min(tsb), min(tse), min(sessionid), min(out), min(info), sum(frequency)
FROM xonsh_history
GROUP BY 1
-- -- Execute carefully:
-- DELETE * FROM xonsh_history;
-- INSERT INTO xonsh_history SELECT * FROM xonsh_history_new;
-- DROP TABLE xonsh_history_new; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
anki-code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My solution for xonsh 0.9.24 on Ubuntu:
$XONSH_HISTORY_BACKEND = 'sqlite'
to.xonshrc
~/.local/share/xonsh/xonsh-history.sqlite
and exit from all xonsh sessions.cd ~/.local/share/xonsh
, runvim json2sqlite.xsh
, copy and paste: