Postgresql and Underscore

TL;DR: Underscore is a matching character in LIKE queries to match a single character. I’m using Postgresql for one of my projects to store some python package metadata. Canonical python packages must be all lowercase and should not use underscores (_) but instead should use hyphens (-) as delimiters. It is just a recommendation and not enforced by PyPI or pip. I wanted to check how many packages in my database were using underscores....

December 20, 2023 · 1 min

Finding the most frequent value in a SQLite column

I have a table with a list of trails that I’ve biked. I wanted to find out which trail I’ve biked the most. I used the following SQL query to find out: SELECT name, count(name) AS ct FROM trails GROUP BY name ORDER BY ct DESC LIMIT 5 This gave me the 5 trails I’ve biked the most. Translated to SQLAlchemy. from sqlalchemy import func def most_common_trails(db: Session): return ( db....

June 9, 2023 · 1 min

Updating a SQLite row with a value from another row

I had a table with a bunch of users and one of the users is a demo user and I wanted to copy the login token from my row to the demo user. This is how I managed to do that. UPDATE users SET token = (SELECT token FROM users u WHERE u.id = 1) WHERE id = 2; This will update the token column for the user with the id 2 with the value from the user with the id 1....

June 9, 2023 · 1 min

Triggering multiple api calls in HTMX

I am a big fan of htmx and use it in a lot of my projects. I recently had a use case where I needed to trigger multiple api calls in a single htmx request. I was able to do this by using the hx-trigger attribute. <html> <head> <script src="https://unpkg.com/htmx.org@1.9.2" integrity="sha384-L6OqL9pRWyyFU3+/bjdSri+iIphTN/bvYyM37tICVyOJkWZLpP2vGn6VUEXgzg6h" crossorigin="anonymous"></script> </head> <body> <input id="input-box" type="text" placeholder="Enter your input and press Enter" name="name"> <div hx-get="/api/call1" hx-trigger="keyup delay:200ms from:#input-box"> </div> <div hx-get="/api/call2" hx-trigger="keyup delay:200ms from:#input-box"> </div> <div hx-get="/api/call3" hx-trigger="keyup delay:200ms from:#input-box"> </div> </body> </html> The idea is to listen to the keyup event on the input box and trigger the api calls after a delay of 200ms....

May 29, 2023 · 1 min