Skip to content

Commit 1632cbe

Browse files
committed
doc: add examples for sql plugin.
Prompted by @ShahanaFarooqui's playing with examples and finding common errors. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 502ff2a commit 1632cbe

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

doc/lightning-sql.7.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,112 @@ The object may contain **warning_db_failure** if the database fails partway thro
354354

355355
On failure, an error is returned.
356356

357+
EXAMPLES
358+
--------
359+
Here are some example using lightning-cli. Note that you may need to
360+
use `-o` if you use queries which contain `=` (which make
361+
lightning-cli(1) default to keyword style):
362+
363+
Two simple peer selection queries:
364+
365+
```
366+
$ lightning-cli sql "SELECT id FROM peers"
367+
{
368+
"rows": [
369+
[
370+
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00"
371+
]
372+
]
373+
}
374+
$ lightning-cli sqlsimple -k columns='["id"]' query="FROM peers"
375+
{
376+
"rows": [
377+
{
378+
"id": "02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00"
379+
}
380+
]
381+
}
382+
```
383+
384+
A statement containing using `=` needs `-o`:
385+
386+
```
387+
$ lightning-cli sql -o "SELECT node_id,last_timestamp FROM nodes WHERE last_timestamp>=1669578892"
388+
{
389+
"rows": [
390+
[
391+
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00",
392+
1669601603
393+
]
394+
]
395+
}
396+
```
397+
398+
If you want to compare a BLOB column, `x'hex'` or `X'hex'` are needed:
399+
400+
```
401+
$ lightning-cli sql -o "SELECT nodeid FROM nodes WHERE nodeid != x'03c9d25b6c0ce4bde5ad97d7ab83f00ae8bd3800a98ccbee36f3c3205315147de1';"
402+
{
403+
"rows": [
404+
[
405+
"0214739d625944f8fdc0da9d2ef44dbd7af58443685e494117b51410c5c3ff973a"
406+
],
407+
[
408+
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00"
409+
]
410+
]
411+
}
412+
$ lightning-cli sql -o "SELECT nodeid FROM nodes WHERE nodeid IN (x'03c9d25b6c0ce4bde5ad97d7ab83f00ae8bd3800a98ccbee36f3c3205315147de1', x'02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00')"
413+
{
414+
"rows": [
415+
[
416+
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00"
417+
],
418+
[
419+
"03c9d25b6c0ce4bde5ad97d7ab83f00ae8bd3800a98ccbee36f3c3205315147de1"
420+
]
421+
]
422+
}
423+
```
424+
425+
Related tables are usually referenced by JOIN:
426+
427+
```
428+
$ lightning-cli sql -o "SELECT nodeid, alias, nodes_addresses.type, nodes_addresses.port, nodes_addresses.address FROM nodes INNER JOIN nodes_addresses ON nodes_addresses.row = nodes.rowid"
429+
{
430+
"rows": [
431+
[
432+
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00",
433+
"YELLOWWATCH-22.11rc2-31-gcd7593b",
434+
"dns",
435+
7272,
436+
"localhost"
437+
],
438+
[
439+
"0214739d625944f8fdc0da9d2ef44dbd7af58443685e494117b51410c5c3ff973a",
440+
"HOPPINGSQUIRREL-1rc2-31-gcd7593b",
441+
"dns",
442+
7171,
443+
"localhost"
444+
]
445+
]
446+
}
447+
```
448+
449+
Simple function usage, in this case COUNT. Strings inside arrays need
450+
", and ' to protect them from the shell:
451+
452+
```
453+
$ lightning-cli sqlsimple '["count(*)"]' "FROM nodes"
454+
{
455+
"rows": [
456+
{
457+
"count(*)": 3
458+
}
459+
]
460+
}
461+
```
462+
357463
AUTHOR
358464
------
359465

0 commit comments

Comments
 (0)