In SQLAlchemy, come faccio a popolare o aggiornare una tabella da una dichiarazione SELECT
?Come INSERIRE IN t1 (SELECT * FROM t2) in SQLAlchemy?
risposta
SQLalchemy non crea questo costrutto per te. È possibile utilizzare la query dal testo.
session.execute('INSERT INTO t1 (SELECT * FROM t2)')
EDIT:
Più di un anno dopo, ma d'ora in poi sqlalchemy 0.6+ you can create it:
from sqlalchemy.ext import compiler
from sqlalchemy.sql.expression import Executable, ClauseElement
class InsertFromSelect(Executable, ClauseElement):
def __init__(self, table, select):
self.table = table
self.select = select
@compiler.compiles(InsertFromSelect)
def visit_insert_from_select(element, compiler, **kw):
return "INSERT INTO %s (%s)" % (
compiler.process(element.table, asfrom=True),
compiler.process(element.select)
)
insert = InsertFromSelect(t1, select([t1]).where(t1.c.x>5))
print insert
produce:
"INSERT INTO mytable (SELECT mytable.x, mytable.y, mytable.z FROM mytable WHERE mytable.x > :x_1)"
Un altro MODIFICA:
Ora, 4 anni dopo, la sintassi è incorporata in SQLAlchemy 0.9 e backportata a 0.8.3; È possibile creare qualsiasi select()
e quindi utilizzare il nuovo metodo di from_select()
Insert
oggetti:
>>> from sqlalchemy.sql import table, column
>>> t1 = table('t1', column('a'), column('b'))
>>> t2 = table('t2', column('x'), column('y'))
>>> print(t1.insert().from_select(['a', 'b'], t2.select().where(t2.c.y == 5)))
INSERT INTO t1 (a, b) SELECT t2.x, t2.y
FROM t2
WHERE t2.y = :y_1
Come Noslko sottolineato in commento, ora è possibile sbarazzarsi di SQL prime: http://www.sqlalchemy.org/docs/core/compiler.html#compiling-sub-elements-of-a-custom-expression-construct
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import Executable, ClauseElement
class InsertFromSelect(Executable, ClauseElement):
def __init__(self, table, select):
self.table = table
self.select = select
@compiles(InsertFromSelect)
def visit_insert_from_select(element, compiler, **kw):
return "INSERT INTO %s (%s)" % (
compiler.process(element.table, asfrom=True),
compiler.process(element.select)
)
insert = InsertFromSelect(t1, select([t1]).where(t1.c.x>5))
print insert
produce:
INSERT INTO mytable (SELECT mytable.x, mytable.y, mytable.z FROM mytable WHERE mytable.x > :x_1)
Ora non è necessario creare il proprio ClauseElement. Puoi usare il nuovo metodo 'Insert.from_select'! Vedi la mia risposta. – nosklo
Come di 0,8. 3, ora puoi farlo direttamente in sqlalchemy: Insert.from_select:
sel = select([table1.c.a, table1.c.b]).where(table1.c.c > 5)
ins = table2.insert().from_select(['a', 'b'], sel)
Grazie. Lo aggiungerò alla risposta originale. – nosklo
- 1. Come è stata implementata la Tupla <T1,T2>. Crea <T1,T2> (T1 item1, T2 item2)?
- 2. Come fare "INSERIRE IN tabella1 (...) SELECT (...) FROM table2" in LINQ?
- 3. inserire utilizzando select distinto
- 4. Conversione tra Tuple <T1,T2> e KeyValuePair <T1, T2>
- 5. Abbina automaticamente le colonne in INSERT INTO ... SELECT ... FROM
- 6. Selezionare un dizionario <T1, T2> con LINQ
- 7. SELECT SUM() FROM (SELECT (SELECT())
- 8. select count (*) from select
- 9. Caratteri di tipo C++ da selezionare tra T1 e T2
- 10. non dovrebbe essere std :: pair <T1,T2> hanno un costruttore di default banale se T1 e T2 hanno?
- 11. JPA - MAX di COUNT o SELECT FROM SELEZIONA
- 12. Reflection MemberInfo to Func <T1, T2>
- 13. C# Convert Func <T1, object> a Funz <T1, T2>
- 14. Migrazione da t1.micro a t2.micro Amazon AWS
- 15. Inserire in unione
- 16. GROUP BY in UPDATE FROM
- 17. Come inserire da select in sequelize
- 18. Perché \ G in SELECT * FROM nome_tabella \ G?
- 19. Come inserire il valore NULL in SQLAlchemy?
- 20. Selezionare come in sqlalchemy
- 21. Equivalente del contenitore C++ STL "coppia <T1, T2>" in Objective-C?
- 22. jQuery remove options from select
- 23. Come inserire più istruzioni select in una tabella temporanea
- 24. Come selezionare i valori di soglia T1 e T2 per Canopy Clustering?
- 25. Come inserire variabile all'interno un'istruzione Select
- 26. implementare creare SELECT nella Impala
- 27. La vista SELECT contiene una sottoquery nella clausola FROM
- 28. "SELECT COUNT (column)" più veloce/più lento di "SELECT COUNT (*)"?
- 29. Come inserire più valori con sottoquery utilizzando SQLAlchemy Core?
- 30. Codeigniter JOIN (query SELECT
Consiglieresti session.execute ('INSERT INTO t1 (% s)' % str (sqlalchemy_select_expression))? – joeforker
Certo, perché no, non ho bisogno di 'str()', dato che '% s' lo fa già. – nosklo
Non è ancora fattibile ora? – Hadrien