News for hackers. Stuff that matters.
Re:Simple SQL in Python by jdnier on Thursday August 13, 2020 @02:24PM (#24146488)
My initial thought is I'd rather just have a module that would find project-related .sql files and parse into named blocks based on the comments, treating them as named strings. No db entanglement required, just a simple way to organize queries in sql files. You would use it more like this:

    import sqlite3
    import project_queries as queries  # a module that loads queries from local .sql files as strings in module namespace

    conn = sqlite3.connect('myapp.db')
    
    cursor = conn.cursor(queries.get_all_users)
    #                    ^^^^^^^^^^^^^^^^^^^^^
    
    users = cursor.fetchall()
    # >>> [(1, "nackjicholson", "William", "Vaughn"), (2, "johndoe", "John", "Doe"), ...]
aiosql may add some features to help with passing values to the query. I see `^` used with `:users` in an example but don't quite get it.

    -- name: get-user-by-username^
I often need to generate DDL details like table and column names in the query, which you don't want escaped, along with data like values and ids where you would want escaping (:users or %(users)s).

It works nicely to use string formatting (`CREATE TABLE {table_name} ...`) for DDL along with interpolation (`WHERE user_id IN %(user_ids)s`) in the same query.

Re:Simple SQL in Python by cheez on Thursday August 13, 2020 @02:47PM (#24146782)
Re:Simple SQL in Python by jdnier on Thursday August 13, 2020 @03:29PM (#24147371)
Perfect, thank you! I wasn't aware of importlib.resources.
Re:Simple SQL in Python by cheez on Thursday August 13, 2020 @05:08PM (#24149197)
It's new. A good habit I've developed recently is to read all the PEPs since the last thing, and all the library changes. Python 3 is in full swing and some great things are in there already
Re:Simple SQL in Python by hprotagonist on Thursday August 13, 2020 @06:50PM (#24150616)
it’s only a decade old or so ;)
Re:Simple SQL in Python by cheez on Thursday August 13, 2020 @08:05PM (#24151531)
Oh it was a fun ten years
Re:Simple SQL in Python by hprotagonist on Thursday August 13, 2020 @09:26PM (#24152125)
i know how lucky i was to only start on 3.5. Avoided a LOT of pain.
Re:Simple SQL in Python by cheez on Thursday August 13, 2020 @11:47PM (#24153093)
You should check out the typing stuff in 3.8. Combined with mypy, gets a lot of the way there to statically checked python without the hassles of statically typed code.
Re:Simple SQL in Python by jimbokun on Thursday August 13, 2020 @02:51PM (#24146841)
The "^" marks a query returning one or zero values:

https://nackjicholson.github.io/aiosql/defining-sql-queries/...

Re:Simple SQL in Python by kristjansson on Thursday August 13, 2020 @02:37PM (#24146655)
I’ll unhelpfully note that I could swear I’ve seen something that does what you describe, and thought that this project was what I recalled - alas.
Re:Simple SQL in Python by wswope on Thursday August 13, 2020 @07:48PM (#24151350)
AnoSQL is a project along these lines I’ve seen on here before.
Re:Simple SQL in Python by nackjicholson on Thursday August 13, 2020 @11:53PM (#24153125)
I forked aiosql from anosql in order to support asyncio based sql drivers. Huge thanks to Honza Pokorny for starting this adventure.
Re:Simple SQL in Python by kristjansson on Friday August 14, 2020 @12:47PM (#24160181)
That’s the one, thank you!