ZSQL and last_insert_id
What is the right syntax for getting the id of the last recorded inserted with ZSQL methods? I tried this: SELECT LAST_INSERT_ID() AS last_insert_id The resulting error is: Traceback (innermost last): File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 223, in publish_module File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 187, in publish File /usr/local/zope/2.4.4b1/lib/python/Zope/__init__.py, line 226, in zpublisher_exception_hook (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 171, in publish File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/mapply.py, line 160, in mapply (Object: manage_test) File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 112, in call_object (Object: manage_test) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 400, in manage_test (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 378, in manage_test (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 493, in __call__ (Object: last_insert_id) File /usr/local/zope/instance/Products/ZMySQLDA/db.py, line 252, in query OperationalError: (1064, "You have an error in your SQL syntax near 'last_insert_id LIMIT 1000' at line 1"
Hi Chad, I think you are missing the table name in your SQL statement. I use something like this: SELECT CONV(LAST_INSERT_ID(), 10, 10) AS LastId FROM TableName; I use MySQL's CONV() function to convert the LAST_INSERT_ID to a string representation because if I don't, it ends up looking something like 237L when it comes back from the database. Hope that helps, Eric. Chad Nantais wrote:
What is the right syntax for getting the id of the last recorded inserted with ZSQL methods? I tried this: SELECT LAST_INSERT_ID() AS last_insert_id
Depends entirely upon your database and not ZSQL Methods. Read your db documentation. ----- Original Message ----- From: "Chad Nantais" <cnantais@rednaxel.com> To: <zope@zope.org> Sent: Tuesday, February 19, 2002 3:03 PM Subject: [Zope] ZSQL and last_insert_id
What is the right syntax for getting the id of the last recorded inserted with ZSQL methods? I tried this: SELECT LAST_INSERT_ID() AS last_insert_id
The resulting error is: Traceback (innermost last): File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 223, in publish_module File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 187, in publish File /usr/local/zope/2.4.4b1/lib/python/Zope/__init__.py, line 226, in zpublisher_exception_hook (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 171, in publish File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/mapply.py, line 160, in mapply (Object: manage_test) File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 112, in call_object (Object: manage_test) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 400, in manage_test (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 378, in manage_test (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 493, in __call__ (Object: last_insert_id) File /usr/local/zope/instance/Products/ZMySQLDA/db.py, line 252, in query OperationalError: (1064, "You have an error in your SQL syntax near 'last_insert_id LIMIT 1000' at line 1"
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Try: select <id> from <table> where <id> = last_insert_id() I think that will work, if not you may try: select <id> from <table> where <id> = conv(last_insert_id(), 10, 10) Hope this helps, -- Jeffrey D. Peterson Webmaster & Resident Standards Warrior "The Trouble with doing anything right the first time is that nobody appreciates how difficult it was."
-----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Chad Nantais Sent: Tuesday, February 19, 2002 5:04 PM To: zope@zope.org Subject: [Zope] ZSQL and last_insert_id
What is the right syntax for getting the id of the last recorded inserted with ZSQL methods? I tried this: SELECT LAST_INSERT_ID() AS last_insert_id
The resulting error is: Traceback (innermost last): File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 223, in publish_module File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 187, in publish File /usr/local/zope/2.4.4b1/lib/python/Zope/__init__.py, line 226, in zpublisher_exception_hook (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 171, in publish File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/mapply.py, line 160, in mapply (Object: manage_test) File /usr/local/zope/2.4.4b1/lib/python/ZPublisher/Publish.py, line 112, in call_object (Object: manage_test) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 400, in manage_test (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 378, in manage_test (Object: last_insert_id) File /usr/local/zope/2.4.4b1/lib/python/Shared/DC/ZRDB/DA.py, line 493, in __call__ (Object: last_insert_id) File /usr/local/zope/instance/Products/ZMySQLDA/db.py, line 252, in query OperationalError: (1064, "You have an error in your SQL syntax near 'last_insert_id LIMIT 1000' at line 1"
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
participants (4)
-
Andy -
Chad Nantais -
Eric Walstad -
Jeff Peterson