TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Forget about sql in 50 lines of python code

14 点作者 jmgutn超过 11 年前

12 条评论

ecopoesis超过 11 年前
Absolutely, emphatically no.<p>Building SQL strings from user data is a terrible idea. You will have SQL injections. You will compromise your database. I&#x27;m sure someone else will add the obligatory link to the Johnny Droptables xkcd.<p>Just learn SQL. It is just not the hard. And please, please use bind variables.
评论 #6545813 未加载
评论 #6542826 未加载
评论 #6542863 未加载
kirinan超过 11 年前
This doesn&#x27;t do joins, unions or anything remotely complicated that would allow me to &quot;forget&quot; about sql. SQL is a terribly important thing to know (especially if you work anywhere near a database), and can give more than just python code. Your micro orm is pretty nice, and Im not faulting your work which looks good, just saying add more work to it and its an ORM, and even then you shouldn&#x27;t forget about sql. Unless this went over my head as a joke, then carry on.
thinkersilver超过 11 年前
I&#x27;m not expecting to get all the bells and whistles of an ORM in the 50 lines so my comment is on the code presented.<p>You probably want to escape the text.<p>Use the execute(QUERY[,optional parameters]) syntax.<p><a href="http://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python/902836#902836" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;902408&#x2F;how-to-use-variabl...</a>
Aqueous超过 11 年前
The last query I wrote had 6 or 7 joins in it, a union, and calculated the frequency of answer responses for a worldwide survey (tens of thousands of respondents) in 0.04 seconds with a 1 million row table. You can accomplish an awful lot of business logic in a single SQL query, which makes SQL tremendously useful thing to know. On the other hand it is tedious to tweak an object mapper call to give you the result that you want that efficiently.<p>This is useful for abstracting away some routine SQL queries, but I think people think simple selects and inserts are more routine than they are actually are.. If you structure your data layer in a way that takes advantage of the power of the RDMBS I find that the only query here is truly routine is insertion on a single row...<p>We already have a language for expressing relational database queries - SQL. Every time you abstract away this language you risk reducing its expressiveness.
fein超过 11 年前
ORM&#x27;s are for basic CRUD crap.<p>SQL is for queries that matter. My ORM usage is almost entirely limited to: get this record, change these values, save it. Anything involving complex joins&#x2F; unions gets thrown to raw SQL. Better yet, stored functions on the DB side. Postgres is awesome for this.
评论 #6542908 未加载
cjauvin超过 11 年前
I agree with all the comments saying that this is dangerous and not a good idea in general. However I&#x27;ve been doing (and using) something related (more in the spirit of Clojure HoneySQL, as I discovered recently), where the idea is to ease the interop of plain data structures and SQL in a pythonic way:<p><a href="https://github.com/cjauvin/little_pger" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;cjauvin&#x2F;little_pger</a><p>I always got the nagging feeling that I should rather use SQLAlchemy, which probably solves the problem in a more elegant and general way, but this little thing has been working great for me.
daGrevis超过 11 年前
I have always wondered could something like this could be implemented as database backend for Django next to SQLite3, MySQL and Postgres backends. The reason for it would be ability to run tests that uses database w&#x2F;o actually using database (because databases are freaking slow even when they are in memory).
mattgreenrocks超过 11 年前
Micro-ORMs were en vogue for a bit in .NET; I&#x27;m not sure about their status at present.<p>All SQL DSLs seem a bit over-the-top to me; you&#x27;re layering a DSL atop another DSL (SQL), forcing you to understand the former intimately, along with knowing the latter well enough to write decent queries.
vinceguidry超过 11 年前
I fell under the spell of ORM, and used it until I had to generate an xml product feed from a large, complex CRM database. It didn&#x27;t take me long to throw out Sequel and write a gigantic SQL query for huge performance gains. You can do joins in Sequel, but it&#x27;s a pain in the neck.
JulianWasTaken超过 11 年前
For anyone looking for this actually done properly, there&#x27;s already the SQLAlchemy query-builder layer.
buremba超过 11 年前
It&#x27;s most likely a troll.
Keyframe超过 11 年前
On a side note, is SQL the only declarative language widely in use?
评论 #6542782 未加载