Get previous input on sqlite CLI tool - sqlite

I'd like to access my previous inputs on the sqlite CLI tool. In bash it works with the arrow up-key but in after running sqlite3 and pressing that key I'm just getting ^[[A. How can I fix this? Which key lets me use my previous typed commands?
Thank you in advance.

Your sqlite3 binary was not built with support for readline.
Either get one that does. Or you can wrap it with readline via rlwrap.
rlwrap sqlite3

Related

Is it possible to run sqlite dot commands from within Java (Android)?

Is it possible to run sqlite dot commands, e.g. .backup / .restore from Java (Android) with for example SqliteOpenHelper or something else?
Currently I have to run the .backup command using shell using sqlite3 binary and I don't want to do that for efficiency reasons. Also it is cumbersome.
Otherwise, any other way to backup and restore from inside Java / Android?

How do I run SQL commands through Notepad++?

I am trying to start out using Notepad++ to run SQLite commands. I have tried following two brief YouTube tutorials to get me going. I can run the initial .bat file, but still cannot run the .sql file.
I have a Windows system environment Path variable set to the folder containing sqlite3.exe
"C:\Users\Adam\sqlite\"
I have saved the following file RunSQLite.bat in the folder containing sqlite3.exe
sqlite3.exe testDB.db
I have created a second file queries.sql
SELECT 34;
When I try to run queries.sql from Notepad++, using the RUN command:
C:\Users\Adam\sqlite\RunSQLite.bat "$(FULL_CURRENT_PATH)"
the only file that appears to run is RunSQLite.bat, giving the output:
SQLite version 3.36.0 2021-06-18 18:36:39
Enter ".help" for usage hints.
sqlite>
Can anyone tell where I have gone wrong?
Thanks in advance.
aphopk
This C:\Users\Adam\sqlite\RunSQLite.bat "$(FULL_CURRENT_PATH)" will do exactly the same thing if run at the shell. RunSQLite.bat does not take any arguments so the Run command in npp is working as expected.
sqlite3 takes input from an external file with the .read command.
Path issues notwithstanding a bat file something like this should accomplish the task:
sqlite3.exe testDB.db ".read %1"
Notepad++ is a text editor, so you can now use it to edit your SQL file. After selecting the Language > SQL, Notepad++ will highlight SQL syntax as you type. Try typing some SQL, like
SELECT "Hi";
SELECT * FROM mydatabase WHERE id LIKE 'ID%';
You will see color, bold, and other possible formatting applied to the text you type. If you save the file as something.sql, and then load something.sql in your SQL client, the client will run the SQL commands from that file. If you have an existing somethingElse.sql file, you can open it in Notepad++, which will auto-recognize that it’s SQL and apply the syntax highlighting, allowing you to edit it and save it.
By using the Run > Run dialog, you can run an arbitrary command. For example, if your SQL client has a command-line mode accessed thru sqlclient.exe, you could type
c:\path\to\sqlclient.exe $(FILE_NAME)
If you just run it, that probably won’t show you any results… but if you ran
cmd /k c:\path\to\sqlclient.exe $(FILE_NAME)
It will open a new cmd.exe Windows command prompt, and show the output from that file.
If instead of running, you hit “SAVE”, you can give it a name (which will end up later in the Run menu), and/or a keyboard shortcut, so that you can easily re-use that many times.
If you want to do something more fancy, use the NppExec plugin, which includes a better batch/scripting language. Once again, you can save the NppExec script, and make it show up in the Macro menu.
If Python is a programming language you know or could learn (or if, like me, you know enough other programming languages that you can fake the Python), then the Python Script plugin will allow you to do even fancier stuff. (Python is a complete programming language, and has many libraries written, which could act as an interface between your SQL source file and your database engine; PythonScript has access to a full python2.7 interpreter. For example, you could write a script in Python which executes the commands from your SQL, grabs the results from your database engine, and displays them in Notepad++, either inline with your original SQL code, or in a new text document. You are really limited only by your imagination and knowledge of Python.)

How to run a sqlite script from Emacs

I'd like to edit a sqlite script in an emacs buffer. I wonder how to run this script(or buffer) from emacs.
Any good idea for this? Thanks.
sqlite is supported out of box in the built-in sql-mode that is automatically enabled for files with .sql extension. You can press M-x sql-sqlite to get access to your sqlite database.
After that you can use commands like C-c C-r to send the region to interpreter and see the results (there are also other commands like send paragraph, send buffer, etc. - see them in the SQL menu.
EmacsWiki has a page about this mode where you can find more recipes, etc.

Comparing 2 SQLite databases

I would like to compare 2 SQLite databases in order to check whether it needs to be updated on the client computer.
I am not really sure how I should do this.
Whether I should make an internal version ID or compare the file size (which probally is not a good idea because I think the file size doesn't change anytime I edit the database).
Does anybody know a good way to do what I need to do?
Thank you!
You might try dumping each SQLite database, using SQLite's .dump command:
$ sqlite3 /path/to/database1 .dump > database1.sql
$ sqlite3 /path/to/database2 .dump > database2.sql
And then comparing the generated files. If your two databases are quite similar, a simple diff might work to show any differences, e.g.:
$ diff -u database1.sql database2.sql
Hope this helps!
A bit late for original question but may be will be helpful for others.
On Windows you can use KS DB Merge Tools for SQLite. I am the author of this tool. Compares both DB object definitions and data. Free version available and pretty functional for diff/compare needs.
EDIT 2022-06-29
I've made the web wasm-based diff-only version of this tool: https://ksdbmerge.tools/for-sqlite-online
EDIT 2022-02-21:
Accepted .dump answer is good for many cases, but it can report false-positive changes. Here is dump result for this question:
Both columns are part of the primary key, so from the point of data model data is the same but dumps are different. My tool is using primary key to compare data so no changes would be reported for such case.
You could use sqldiff.
On Debian or Debian-based systems (and possibly other Linux systems) it is installed by default when installing sqlite3: apt install sqlite3
On MacOS, it can be installed with Homebrew : brew install sqldiff

Getting started with SQLite on windows, why isn't it outputting anything?

I've just started using SQLite, following the tutorials here and I'm using this on windows. I've got sqlite3.exe extracted to a folder on my desktop, and am running the following line in a command prompt to test it:
sqlite3 test.db
which the official documentation claims should create a file called test.db to work with. However, it doesn't appear to be doing anything, no files are created and it gives no errors or success messages, just another sqlite> prompt. Am I missing something to get this working on windows 7? Thanks.
If the database file does not exist, SQLite starts with an empty database.
The file itself will not be created until you actually write to the database.
Try creating something:
CREATE TABLE Test(some stuff);

Resources