SQLite - reverse string function
I solved my problem storing word reverses which I obtained via this PHP script:
class MyDB extends SQLite3
{
function __construct()
{
$dbFile = __DIR__ . '/Dictionary.sqlite';
$this->open($dbFile);
}
}
$db = new MyDB();
$db->createFunction('rev', 'strrev', 1);
$db->exec('UPDATE dict_en SET word_rev = rev(word)');
echo "done";
Using a common table expression it is possible to reverse a string in SQLite.
WITH reverse(i, c) AS (
values(-1, '')
UNION ALL SELECT i-1, substr('dlrow olleh', i, 1) AS r FROM reverse
WHERE r!=''
) SELECT group_concat(c, '') AS reversed FROM reverse;
Returns hello world
.
There is no builtin function for that. You can add custom function, like in this example in Python:
import sqlite3
conn = sqlite3.connect("")
conn.create_function("strrev", 1, lambda s: s[::-1])
cur = conn.cursor()
cur.execute(r''' SELECT strrev('hello, world') ''')
print(cur.fetchone()[0]) #dlrow ,olleh