Mike
Mike

Reputation: 24393

URL encoding in SQLite

Is there a way to URL encode fields in SQLite directly in the query?

(somehow this question doesn't meet "quality standards" so I'll throw in an example query:)

select urlencode( field ) from table;

Upvotes: 2

Views: 2953

Answers (3)

Anton Zhiyanov
Anton Zhiyanov

Reputation: 382

SQLite does not support URL encoding out of the box.

But you can add URL encoding/decoding functions with the sqlean-crypto extension.

For example:

select load_extension('crypto.dll');

select encode('/search?q=sql+is+awesome', 'url');
-- %2Fsearch%3Fq%3Dsql%2Bis%2Bawesome

select decode('%2Fsearch%3Fq%3Dsql%2Bis%2Bawesome', 'url');
-- /search?q=sql+is+awesome

The extension also supports other encoding algorithms like Base32, Base64 and Base85.

Upvotes: 3

Hussein mahyoub
Hussein mahyoub

Reputation: 482

If the target is web browser, you can use javascript to achieve that like below:

select '<a href=# onclick=''window.open("https://api.whatsapp.com/send?text="+encodeURIComponent(" Welcome '||CustomerName||'"), "_blank").focus();''>wtsup</a>' wtsup2 from customer_table ; 

Upvotes: -2

Sjlver
Sjlver

Reputation: 1295

Looking through the sqlite function list, I didn't find anything that would do the job. But you can construct such a function manually as follows:

select replace(replace(replace(replace(field,
    " ", "%20"),
    "/", "%2F"),
    "(", "%28"),
    ")", "%29")
from table;

This version escapes four of the more common problematic characters. It's a quick-and-dirty solution if your data set does not contain many unsafe characters.

Upvotes: 5

Related Questions