dbcli / litecli

CLI for SQLite Databases with auto-completion and syntax highlighting
https://litecli.com
BSD 3-Clause "New" or "Revised" License
2.12k stars 68 forks source link

Feature request: support more parameter templates recognized by sqlite3_bind #45

Closed zmwangx closed 5 years ago

zmwangx commented 5 years ago

Favorite queries seem to be very useful as poor man's prepared statements, which sqlite3 CLI lacks. However, right now only shell-style parameter templates $1, $2, etc. are allowed. It would be nice to support more templates recognized by sqlite3_bind(), so that prepared statements can be directly taken from code or logs. At the very least, ? would be immensely helpful.

amjith commented 5 years ago

@zmwangx Can you post a couple of examples of prepared SQL statements. It is not obvious from the docs page.

zmwangx commented 5 years ago

Sure, I'm talking about statements like

INSERT INTO user (first_name, last_name, email, registered_at) VALUES (?,?,?,?);
SELECT * FROM user WHERE email = ?;
SELECT * FROM user WHERE registered_at BETWEEN ? AND ?;

You know, parametrized SQL that is commonly used in language bindings (e.g. Python's sqlite3 module). ? is just the simplest form, and probably the easiest to implement.


https://litecli.com/favorites/ gives this example:

\fs user_by_name select * from users where name = '$1'

It would be nice to be able to write

\fs user_by_name select * from users where name = ?

instead.

amjith commented 5 years ago

That should be doable, I have a vague remembrance of supporting this in pgcli or mycli and then we took it out for some reason. I'll dig up the old commits to see if the reasoning is still valid, if not, I'll take a stab at implementing this.

zmwangx commented 5 years ago

Cool, thanks for the effort.

amjith commented 5 years ago

This is now released as part of 1.1.0. Please upgrade using pip install -U litecli.