TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Simple SQL in Python

233 pointsby polyrandalmost 5 years ago

22 comments

viialmost 5 years ago
The syntax follows <a href="https:&#x2F;&#x2F;github.com&#x2F;krisajenkins&#x2F;yesql&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;krisajenkins&#x2F;yesql&#x2F;</a> which works with Clojure. So then the DB queries can be defined in a language agnostic way, so you don&#x27;t need a gateway API to access the DB :)<p>There are many opportunities to grow this, e.g. transforming the SQL files into stored procedures, and being able to lint and check against a DB schema definition. However, I&#x27;m not sure that the indirection step of hiding the SQL away from the code actually makes sense in the long run. A tight coupling allows filters and other optimisations to be easily added to the end of a query, and saves one lookup that is not yet supported in your editor.
评论 #24153113 未加载
hprotagonistalmost 5 years ago
Seems similar in inspiration to <a href="https:&#x2F;&#x2F;pugsql.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pugsql.org&#x2F;</a> which is a python reimplementation of hugSQL, more or less.<p>I&#x27;d say an advantage of pugSQL is that it&#x27;s sqla-core under the hood, so lots of stuff will Just Work and you can drop down to core to commit war crimes if you have to.
评论 #24148959 未加载
评论 #24153163 未加载
评论 #24146357 未加载
评论 #24144616 未加载
ThePhysicistalmost 5 years ago
Small nitpick: SQLAlchemy is not just an ORM, it provides a lot of functionality to interact with SQL databases without mapping results to objects.
评论 #24145233 未加载
评论 #24146599 未加载
kristapsalmost 5 years ago
No interest in the tool itself, but love the neutral outline of use cases it&#x27;s a good&#x2F;bad fit for - more software should do this.
icholyalmost 5 years ago
Go has something similar to this <a href="https:&#x2F;&#x2F;github.com&#x2F;kyleconroy&#x2F;sqlc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kyleconroy&#x2F;sqlc</a>
jdnieralmost 5 years ago
My initial thought is I&#x27;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:<p><pre><code> import sqlite3 import project_queries as queries # a module that loads queries from local .sql files as strings in module namespace conn = sqlite3.connect(&#x27;myapp.db&#x27;) cursor = conn.cursor(queries.get_all_users) # ^^^^^^^^^^^^^^^^^^^^^ users = cursor.fetchall() # &gt;&gt;&gt; [(1, &quot;nackjicholson&quot;, &quot;William&quot;, &quot;Vaughn&quot;), (2, &quot;johndoe&quot;, &quot;John&quot;, &quot;Doe&quot;), ...] </code></pre> aiosql may add some features to help with passing values to the query. I see `^` used with `:users` in an example but don&#x27;t quite get it.<p><pre><code> -- name: get-user-by-username^ </code></pre> I often need to generate DDL details like table and column names in the query, which you don&#x27;t want escaped, along with data like values and ids where you would want escaping (:users or %(users)s).<p>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.
评论 #24146782 未加载
评论 #24146841 未加载
评论 #24146655 未加载
评论 #24152454 未加载
welderalmost 5 years ago
Nice idea, but this pattern hides your queries away from the developer. In practice, developers need the query transparent and not hidden in a file somewhere away from the code they&#x27;re reading. Otherwise you&#x27;re building a pattern for using queries without knowing the query&#x27;s cost. If you need raw SQL, use SQLAlchemy Core and just don&#x27;t use the ORM features.
评论 #24146263 未加载
评论 #24145313 未加载
评论 #24145961 未加载
评论 #24145587 未加载
评论 #24146132 未加载
评论 #24146123 未加载
评论 #24145396 未加载
评论 #24145367 未加载
评论 #24145492 未加载
krickalmost 5 years ago
Looks kinda nice, but can it handle &quot;IN (...)&quot; statements correctly? Or conditionally adding some WHERE param? If not, this is rather simplistic and couldn&#x27;t replace writing inline queries, and then I think I&#x27;d rather not mix together 2 approaches.
评论 #24149144 未加载
评论 #24152541 未加载
bbsimonbbalmost 5 years ago
SQL should have been in .sql files from the start. SQL in string literals is an industry-wide decades long aberration. I fixed this problem for C# with QueryFirst. I&#x27;m delighted to see all the support here the approach, which is clearly the way forward for data access.<p><a href="https:&#x2F;&#x2F;marketplace.visualstudio.com&#x2F;items?itemName=bbsimonbb.QueryFirst&amp;ssr=false#overview" rel="nofollow">https:&#x2F;&#x2F;marketplace.visualstudio.com&#x2F;items?itemName=bbsimonb...</a>
elamjealmost 5 years ago
I really like this approach because as you said, it gets really messy to do string replacements inline in a python file.<p>I&#x27;m curious if any people here are comfortable using stored procedures as an alternative to this.<p>Stored procs give you the benefit of your sql being easy to change in a sql editor, but you also get query planner caching of the results which means it will likely execute faster than string replacement inline sql.
评论 #24149719 未加载
stasaalmost 5 years ago
I&#x27;m relatively new to Python and I was looking for something like this. I do wonder if there is a well-known Python equivalent of Dapper: <a href="https:&#x2F;&#x2F;github.com&#x2F;StackExchange&#x2F;Dapper" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;StackExchange&#x2F;Dapper</a>
评论 #24145296 未加载
iwebdevfromhomealmost 5 years ago
Looks like a cool idea. I wonder if I can make it to work easily with this other lovely python library <a href="https:&#x2F;&#x2F;github.com&#x2F;pudo&#x2F;dataset" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pudo&#x2F;dataset</a>
评论 #24148564 未加载
stdohmalmost 5 years ago
I&#x27;m a fan of using raw sql like this but curious if anyone has solved deduplicating common statements and dynamic queries in a good way.<p>For example:<p>1) Imagine lots of queries having to show the results in the context of a user and therefore use the same JOIN and WHERE clause all over. Not being DRY, this breaks down when having to change the clause at all.<p>2) Imagine a reporting page that allows for filtering and ordering by different columns and therefore need some way to compose the final sql.
评论 #24148074 未加载
grncdralmost 5 years ago
I&#x27;ve always wanted to build a larger app based on this approach. I quite like Massive, and 5 years (!) ago I wrote <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;grncdr&#x2F;4555208#file-sss-py" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;grncdr&#x2F;4555208#file-sss-py</a> as a one-off response to a tweet from Ted Dziuba (is he still around?)
devxpyalmost 5 years ago
Nice approach with annotations in the comments.<p>I wrote something a little more tounge in cheek a while ago -- I modified SQL syntax to include functions with arguments<p><a href="https:&#x2F;&#x2F;github.com&#x2F;scientifichackers&#x2F;sql2code" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;scientifichackers&#x2F;sql2code</a>
jessedhillonalmost 5 years ago
Anyone who is attracted to this approach should look into stored procedures instead: <a href="https:&#x2F;&#x2F;www.postgresqltutorial.com&#x2F;postgresql-create-procedure&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.postgresqltutorial.com&#x2F;postgresql-create-procedu...</a>
评论 #24145812 未加载
didipalmost 5 years ago
I like this idea a lot actually. I&#x27;ve been pondering of doing the same thing in Go.<p>There are some edges though... for example what if you want to do further composition based on if&#x2F;else clause.
评论 #24202289 未加载
qaqalmost 5 years ago
Funny to see on HN just started building a version of this that actually generates functions from sql queries so you get all the IDE goodness and sightly smaller runtime overhead
tracnaralmost 5 years ago
This could go one step further by implementing an import hook, then you could do import users; users.get_all_users(conn)<p>It&#x27;s something I&#x27;ve been meaning to try with other languages.
评论 #24202413 未加载
emrealmost 5 years ago
Ibis Project is also similar: <a href="https:&#x2F;&#x2F;ibis-project.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ibis-project.org&#x2F;</a>
delducaalmost 5 years ago
<a href="https:&#x2F;&#x2F;github.com&#x2F;mcfunley&#x2F;pugsql" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mcfunley&#x2F;pugsql</a>
darwinwhyalmost 5 years ago
<a href="https:&#x2F;&#x2F;github.com&#x2F;pudo&#x2F;dataset" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pudo&#x2F;dataset</a>