{"id":7583,"date":"2015-08-22T11:39:19","date_gmt":"2015-08-22T11:39:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/08\/22\/how-to-execute-raw-sql-in-sqlalchemy-flask-app-open-source-projects-mitsuhiko-flask\/"},"modified":"2015-08-22T11:39:19","modified_gmt":"2015-08-22T11:39:19","slug":"how-to-execute-raw-sql-in-sqlalchemy-flask-app-open-source-projects-mitsuhiko-flask","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/08\/22\/how-to-execute-raw-sql-in-sqlalchemy-flask-app-open-source-projects-mitsuhiko-flask\/","title":{"rendered":"How to execute raw SQL in SQLAlchemy-flask app-open source projects mitsuhiko\/flask"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/nS1qF.jpg?s=128&amp;g=1\" \/> <strong>jberger<\/strong><\/p>\n<p>docs: SQL Expression Language Tutorial &#8211; Using Text<\/p>\n<p>example:<\/p>\n<pre><code>from sqlalchemy.sql import text\n\nconnection = engine.connect()\n\n# recommended\ncmd = 'select * from Employees where EmployeeGroup == :group'\nemployeeGroup = 'Staff'\nemployees = connection.execute(text(cmd), group = employeeGroup)\n\n# or - wee more difficult to interpret the command\nemployeeGroup = 'Staff'\nemployees = connection.execute(\n                  text('select * from Employees where EmployeeGroup == :group'), \n                  group = employeeGroup)\n\n# or - notice the requirement to quote \"Staff\"\nemployees = connection.execute(\n                  text('select * from Employees where EmployeeGroup == \"Staff\"'))\n\n\nfor employee in employees: logger.debug(employee)\n# output\n(0, u'Tim', u'Gurra', u'Staff', u'991-509-9284')\n(1, u'Jim', u'Carey', u'Staff', u'832-252-1910')\n(2, u'Lee', u'Asher', u'Staff', u'897-747-1564')\n(3, u'Ben', u'Hayes', u'Staff', u'584-255-2631')\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jberger docs: SQL Expression Language Tutorial &#8211; Using Text example: from sqlalchemy.sql import text connection = engine.connect() # recommended cmd = &#8216;select * from Employees where EmployeeGroup == :group&#8217; employeeGroup = &#8216;Staff&#8217; employees = connection.execute(text(cmd), group = employeeGroup) # or &#8211; wee more difficult to interpret the command employeeGroup = &#8216;Staff&#8217; employees = connection.execute( text(&#8216;select [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7583","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7583"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7583\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}