python - How do I INSERT INTO t1 (SELECT * FROM t2) in SQLAlchemy? -


How can I populate or update a table with a SELECT statement?

SQLalchemy does not create it for you You can use the query from the text.

  add session.execute (Add to T1 (SELECT * FROM T2) ')  

Edit:

Sqlalchemy.sql.expression Import Executable, ClauseElement Class InsertFromSelect (Executable, ClauseElement) from more than a year later, but now sqlalchemy.ext import compiler from 0.6+:

  on sqlalchemy: Def __init __ (self, table, selection): self.table = table self.select = select @ compiler.compiles (InsertFromSelect) def visit_insert_from_select (Element, Compiler, ** kw): return "% S in INSERT% s" % (Compiler.process (element.table, asfrom = true), compiler.process (element.select)) Insert = Select Insert (t1, select ([T1]). (T1.cx> 5)) Print  

Production:

  "Insert in mytable (selection mytable. X, mytable.y, mytable.z mytable WHERE mytable.x & Gt ;: x_1) " 

another edit:

now, 4 Years later, Syntax was backed up to SQL Server 0.9 and 0.8.3; You can make any select () and then use the to insert objects with the new to-select () method Can:

  & gt; & Gt; & Gt; Sqlalchemy.sql Import table, column and gt; & Gt; & Gt; T1 = Table ('T1', column ('A'), column ('B')) & Gt; & Gt; T2 = table ('T2', column ('x'), column ('y')) gt; & Gt; & Gt; Print (t1.insert (). Se_selection (['A', 'B'], T. Sealet (). Where (T2 C == 5) T1 (A, B) Type T2 Select X, from T2.y to T2 WHERE t2.y =: y_1  

.


Comments