SQLite Forum

edit() function usage

edit() function usage

(1) By Trudge on 2021-07-08 17:48:38 [link] [source]

I'm running on a Mac and am having some issues with the edit() function. BBEdit is my default editor but I also have the native TextEdit available. I can get it almost working with 'update books set notes=edit(notes,'BBEDIT') WHERE id=1;' which does open BBEdit with the requested document loaded. But when I save and quit the editor it does not update the text in the DB.

Is there something I'm missing?

(2.1) By Larry Brasfield (larrybr) on 2021-07-08 19:26:11 edited from 2.0 in reply to 1 [link] [source]

I don't see anything wrong. (That's what I call "a statement of ignorance.")

Have you looked to see if the file, (which is created as a randomly named file in the process's current directory), is present and modified while the editor is open, after a save but before quitting? (It is automatically deleted after editor terminates and the edit() function has sucked its content, so you must look at the right time. It will be a recently created file.)

Have your tried using 'TextEdit' in place of 'BBEdit'?

I do not have BBEdit on my Macs because they are headless, so I am not prepared to do repro on this. I can say that edit() works fine on Windows and Linux. You might try using edit('notes', 'vi') (or some other not-overly-clever editor.) If you do, please report what happens.

(2.1 amendment:)

FWIW, the edit('', 'mg') function works fine as an insert value on my Mac mini running the last OS X version 10.?. The mg editor is findable on my $PATH and knows how to edit, in place1, a file named as its 1st argument.

1. In-place editing can sometimes be important. There are editors that play games with file names as part of keeping a backup. If more than one process was involved in running an editor, this could interact badly with how edit() works.

(6) By Trudge on 2021-07-08 20:36:17 in reply to 2.1 [link] [source]

Yes, the file exists in /private/var/.... and I can edit it. After saving I then close that file (editor BBEdit). But when I check 'notes' it has not changed.

Using 'TextEdit' it tells me: sh: TextEdit: command not found Error: EDITOR returned non-zero

Then tried 'update books set notes=edit(notes,'nano') WHERE id=1;' which DID work.

So it seems BBEdit is doing something wonky but at least I got it working. I had set Thanks to all for the suggestions.

(3) By Stephan Beal (stephan) on 2021-07-08 19:22:30 in reply to 1 [link] [source]

Is there something I'm missing?

My vague suspicion, based on a similar case once reported in the fossil project, is that your editor is forking and immediately returning to the calling process rather than waiting until the user explicitly exits it.

(4) By Harald Hanche-Olsen (hanche) on 2021-07-08 20:16:19 in reply to 3 [link] [source]

You're probably onto something there. BBEDIT is a GUI application, whereas the edit() command in SQLite expects a path to an executable which will start an editor, and exit when the user is done. Starting a GUI Application on a Mac is a rather complicated affair involving more than a simple fork(), but the principle remains: The process activating the GUI application will usually continue as soon as it has triggered the activation of the application.

If you do this in the interactive SQLite shell, you would probably see the shell prompt immediately after invoking the edit.

For example, I don't have a copy of BBEDIT, but I tried an experiment using an editor called CotEditor (available from the App Store). CotEditor comes with an associated command line utility simply called cot, which will start CotEditor on a given file. It accepts a flag -w, which causes cot to wait until CotEditor has closed the file.

My little experiment proceeded as follows:

▶▶▶ create table foo(bar);
▶▶▶ insert into foo values("blah");
▶▶▶ select rowid,bar from foo;
rowid  bar
-----  ----
1      blah
▶▶▶ update foo set bar=edit(bar,'cot') where rowid=1;
-- note: the prompt below appeared immediately,
-- before I had time to do any editing; so, no change
▶▶▶ update foo set bar=edit(bar,'cot -w') where rowid=1;
-- here, nothing happened until I had saved and closed the temporary file
▶▶▶ select rowid,bar from foo;
rowid  bar
-----  ------
1      blargh
-- success!

If you wish this to work with BBEDIT, you need a similar tool. I don't know if BBEDIT provides such a tool.

(5) By RandomCoder on 2021-07-08 20:22:51 in reply to 3 [source]

Correct, you need to call bbedit with a -w flag to cause the CLI shim to wait for the GUI app to close before exiting.

(7) By Trudge on 2021-07-08 20:43:28 in reply to 5 [link] [source]

I did not know about the CLI for BBEdit but -w does work in Terminal. Tried it in sqlite and worked there as well: update books set notes=edit(notes,'bbedit -w') WHERE id=1;

Thanks @RandomCoder eh!