简体   繁体   中英

Cursor keys not working when using sqlite3 from adb shell

When using sqlite3 through adb shell arrow keys, instead of moving the cursor to the desired position or summoning the history facility, the following is showed in the screen: ^[[A , ^[[B , ^[[C , ^[[D .

I'm using Mac OS X and I have tried Terminal and iTerm terminal emulators.

Does anybody know how to fix this?

To allow editing and history in the input of a console program, that program must be linked with the readline library.

The sqlite3 tool does support readline, but on Android, readline support has been disabled. (Probably because readline is licensed only under the GPL.)

A workaround would be to use a local version of SQLite with readline support.

  1. Copy a database file from your device to your local machine:
    adb pull <database-file-on-device>

  2. Use your local version of SQLite to access the database file:
    sqlite3 <database-file-on-local>

  3. If you made changes you can transfer them to the device. Copy your local database file from your local machine to your device:
    sqlite3 <database-file-on-local> <database-file-on-device>

You can use the previous command functionality in the adb shell. So just adb shell. Then cd to the /data/data//databases directory. From here run (for example): sqlite3 "select * from "

Then you can use up arrow to redo that command. Kind of a hack, but way better than having to retype the command inside the sqlite3 interactive prompt.

Working off of Khanad's answer: I wrote a shell script on the device and I send my sqlite commands through it.

First create a script file on the device and make it executable:

# get on the device
$ adb shell 

# get write access on the device (this will also put you in the data/data directory)
$ run-as com.YOUR_PACKAGE.YOUR_APP_NAME

# create the script file
$ touch qlite

# make it executable
$ chmod +x qlite

Then add this code to the script. Remember to put your actual app name in there. (The echoes just add a little breathing room to the output.)

echo

sqlite3 databases/YOUR_APP_NAME -cmd ".mode column" ".headers on" "$1;"

echo

Then you just have to do something like:

./qlite "select * from table limit 1"

The script will pass the sqlite command through, tack on the semi-colon, pretty print it with column names and you can use the up arrow to get your last command. And because you have write access you can make updates directly to the DB on the device.

I hope this makes someone's life easier!

Use ADB shell instead of sqlite3 shell to get the advantage of the readline package.

generic_x86:/data/data/com.example.musicplayer/databases $ sqlite3 music_player_database -line 'select * from lastplayedsong;'

Just make sure you are in the correct package and don't put .db at the end of the database name otherwise a new database would be created and the query would return you no result.
PS: source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM