Docs update for using functions with insert()

Issue #1791 resolved
Former user created an issue

I'm not a very good docs writer. However, here is my feeble attempt.

--- a/doc/build/sqlexpression.rst       Wed Apr 28 16:58:23 2010 -0500
+++ b/doc/build/sqlexpression.rst       Mon May 03 12:20:55 2010 -0500
@@ -136,6 +136,23 @@
     >>> ins.compile().params #doctest: +NORMALIZE_WHITESPACE
     {'fullname': 'Jack Jones', 'name': 'jack'}

+If you need to make use of SQL functions in your insert statements, you have to make use of the .values() functionality. For example, if you wanted to make use of the (contrived) UPPER function, you could do this:
+
+    >>> ins = users.insert().values( name=func.upper('jack'), fullname='Jack Jones')
+    >>> str(ins)
+    'INSERT INTO users (name, fullname) VALUES (upper(:upper_1), :fullname)'
+
+Even if you don't normally use the .values functionality, to make use of functions you could take a hybrid approach. Making use of execution (see `Executing`_, below):
+
+    >>> conn = engine.connect()
+    >>> ins = users.insert().values( name=func.upper('jack') )
+    >>> conn.execute( ins, { fullname:'Jack Jones'  } )
+    >>> r = conn.execute( ins, { 'fullname':'Jack Jones'  } ) #doctest: +ELLIPSIS
+    {opensql}INSERT INTO users (name, fullname) VALUES (upper(?), ?)
+    ('jack', 'Jack Jones')
+    COMMIT
+    {stop}<sqlalchemy.engine.base.ResultProxy object at 0x...>
+

Comments (1)

  1. Log in to comment