{"id":2884,"date":"2021-07-21T07:42:00","date_gmt":"2021-07-21T07:42:00","guid":{"rendered":"https:\/\/www.bearloga.space\/?page_id=2884"},"modified":"2021-07-21T07:42:00","modified_gmt":"2021-07-21T07:42:00","slug":"yadro-sqlalchemy-ispolzovanie-tekstovogo-sql","status":"publish","type":"page","link":"https:\/\/www.bearloga.space\/uk\/yadro-sqlalchemy-ispolzovanie-tekstovogo-sql\/","title":{"rendered":"\u042f\u0434\u0440\u043e SQLAlchemy \u2014 \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0433\u043e SQL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SQLAlchemy \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438, \u0434\u043b\u044f \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 SQL \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u0438 \u043d\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0435 \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439.&nbsp;\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f text () \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041e\u043d \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u043e\u0435&nbsp;<strong>TextClause<\/strong>&nbsp;, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 SQL \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u043d\u0438\u0436\u0435 \u043a\u043e\u0434\u0435 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import text\nt = text(\"SELECT * FROM students\")\nresult = connection.execute(t)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430&nbsp;<strong>text ()<\/strong>&nbsp;\u043f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u041d\u0435\u0439\u0442\u0440\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438<\/li><li>\u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/li><li>\u0442\u0438\u043f \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u0414\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 text () \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b Bound \u0432 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0435 \u0434\u0432\u043e\u0435\u0442\u043e\u0447\u0438\u044f.&nbsp;\u041e\u043d\u0438 \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u044b \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.&nbsp;\u0427\u0442\u043e\u0431\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0438\u0445 \u0432 \u043c\u0435\u0442\u043e\u0434 execute () \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u043e\u043c SQL <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy.sql import text\ns = text(\"select students.name, students.lastname from students where students.name between &#x1f621; and :y\")\nconn.execute(s, x = 'A', y = 'L').fetchall()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0424\u0443\u043d\u043a\u0446\u0438\u044f text () \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 SQL \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">select students.name, students.lastname from students where students.name between ? and ?<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u044f x = \u2018A\u2019 \u0438 y = \u2018L\u2019 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b.&nbsp;\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0442\u0440\u043e\u043a \u0441 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u2018A\u2019 \u0438 \u2018L\u2019 \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[('Komal', 'Bhandari'), ('Abdul', 'Sattar')]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f text () \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 TextClause.bindparams ().&nbsp;\u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u044f\u0432\u043d\u043e \u0432\u0432\u0435\u0434\u0435\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">stmt = text(\"SELECT * FROM students WHERE students.name BETWEEN &#x1f621; AND :y\")\n\nstmt = stmt.bindparams(\n   bindparam(\"x\", type_= String), \n   bindparam(\"y\", type_= String)\n)\n\nresult = conn.execute(stmt, {\"x\": \"A\", \"y\": \"L\"})\n\nThe text() function also be produces fragments of SQL within a select() object that \naccepts text() objects as an arguments. The \u201cgeometry\u201d of the statement is provided by \nselect() construct , and the textual content by text() construct. We can build a statement \nwithout the need to refer to any pre-established Table metadata. \n\nfrom sqlalchemy.sql import select\ns = select([text(\"students.name, students.lastname from students\")]).where(text(\"students.name between &#x1f621; and :y\"))\nconn.execute(s, x = 'A', y = 'L').fetchall()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e&nbsp;<strong>and_ ()<\/strong>&nbsp;\u0434\u043b\u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0432 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 WHERE, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 text ().<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from sqlalchemy import and_\nfrom sqlalchemy.sql import select\ns = select([text(\"* from students\")]) \\\n.where(\n   and_(\n      text(\"students.name between &#x1f621; and :y\"),\n      text(\"students.id&gt;2\")\n   )\n)\nconn.execute(s, x = 'A', y = 'L').fetchall()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b\u0448\u0435 \u043a\u043e\u0434 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u0438\u043c\u0435\u043d\u0430\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u00abA\u00bb \u0438 \u00abL\u00bb \u0441 id \u0431\u043e\u043b\u044c\u0448\u0435 2. \u0412\u044b\u0432\u043e\u0434 \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435 \u2014<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQLAlchemy \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438, \u0434\u043b\u044f \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 SQL \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u0438 \u043d\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0435 \u0434\u043b\u044f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[103],"tags":[],"class_list":["post-2884","page","type-page","status-publish","hentry","category-sqlalchemy"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/2884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/comments?post=2884"}],"version-history":[{"count":0,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/2884\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/media?parent=2884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/categories?post=2884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/tags?post=2884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}