2

sqlite3 stores command history in .sqlite_history, which is by default created in:

$HOME/.sqlite_history

How can I change this location to somewhere else?

This is possible for example with mysql, where I can define environment variable

MYSQL_HISTFILE=/path/to/whatever/file 

But I could not find any corresponding environment variable for sqlite3

export SQLITE_HISTFILE=/tmp/history 

has no effect. I found a post where somebody asks same question, but no useful answers are given.

Rahul
  • 13,589
Martin Vegter
  • 358
  • 75
  • 236
  • 411
  • 1
    Request was made to the project at http://sqlite.1065341.n5.nabble.com/Customizing-the-location-of-the-sqlite-history-td87055.html – Ben Creasy Sep 12 '16 at 07:45

3 Answers3

6

Since Version 3.25.3 you can simply set SQLITE_HISTORY to change the history filename, like mattmc3 wrote. In the versions before, it was hardcoded in line 5576 in shell.c (version 3.14.1):

sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);

So, to change it, one option among the others mentioned in this thread is was to edit the source and recompile.

Hoov
  • 907
  • 7
  • 13
3

To list some of the possible alternatives,

  1. make ~/.sqlite_history into a symbolic link to another file.

  2. simply run HOME=/tmp sqlite3 to have the program save the history in /tmp/.sqlite_history, though this assumes you don't need the real home directory inside the command environment.

  3. edit the binary and replace the string "%s/.sqlite_history", found by Hoov, by another string with the same number of bytes, eg "/tmp/sqlitehistory":

    sed < /usr/bin/sqlite3 's|%s/.sqlite_history|/tmp/sqlitehistory|' >/tmp/sqlite3
    cmp -l /usr/bin/sqlite3 /tmp/sqlite3 # check no extraneous differences
    chmod +x /tmp/sqlite3
    /tmp/sqlite3
    
  4. use the LD_PRELOAD shim I proposed in an another question to change one filename ("/tmp/adb.log" in that case) to another during an open() call.

meuh
  • 51,383
  • Excellent and enlightening answer. I feel ignorant now. – Hoov Sep 02 '16 at 07:36
  • But in reality, when this situation arises, I usually prefer to follow your answer and rebuild from sources, unless this proves to be too tedious, with too many dependencies etc. – meuh Sep 02 '16 at 07:53
  • Yes, it think it's the most manageable solution, but your solution three could come in handy when you don't have the source. Saved it to a script. – Hoov Sep 02 '16 at 08:07
  • Note that in sqlite's case, setting $HOME will not work, since sqlite does not honour that environment variable: it uses only what getpwuid() returns (e.g. from /etc/passwd). – ayekat Aug 31 '17 at 05:58
3

As of Oct 10, 2018 it appears that a new variable, $SQLITE_HISTORY, was added to address this.

mattmc3
  • 352