syntax error at 'SELECT LAST_INSERT_ID()'


syntax error at 'SELECT LAST_INSERT_ID()'



If I run my query in python, it returns a syntax error.



However, if I run this with navicat, it work's fine.



Why doesn't it run in python?



sql_query


INSERT INTO
solarsystem
(
solarsystemName,
solarsystemPositionX,
solarsystemPositionY,
solarsystemSectorId,
solarsystemAngle
)
VALUES
('Lima [698/562]',698,562,13,171);

SELECT LAST_INSERT_ID();



error



1064, u"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID()'



[EDIT]



create.py


[...]

args = (solarsytem['solarsystemName'], solarsytem['x_pos'], solarsytem['y_pos'],
solarsytem['sector_id'], solarsytem['angle'])

with self.engine.begin() as connection:
result_proxy = connection.execute(sql_query, args).first()

[...]



UPDATE



If I try the INSERT as single query this works.


INSERT



However, SELECT LAST_INSERT_ID(); didn't work. It returns:


SELECT LAST_INSERT_ID();



TypeError: not all arguments converted during string formatting





Help us help you - share your python code too
– Mureinik
Jun 30 at 7:57





Have you tried executing these two statements with two separate execute calls?
– Mureinik
Jun 30 at 8:17


execute





Yes, If i run SELECT LAST_INSERT_ID() FROM solarsystem ; it returns TypeError: not all arguments converted during string formatting ( i run this without args) but in Navicat it always work's
– Luca
Jun 30 at 8:30


SELECT LAST_INSERT_ID() FROM solarsystem ;




1 Answer
1



Do it over your connection to get the last row ID inserted on the cursor object,



You can use


connection.insert_id()



Or if you have a cursor:


cursor.lastrowid



EDIT



For sqlalchemy you can use:


with self.engine.begin() as connection:
result_proxy = connection.execute(sql_query, args).first()
last_id=result_proxy.inserted_primary_key[0]



And you will have your id in last_id


last_id



But you must remove the SELECT LAST_INSERT_ID(); from your sql_query


SELECT LAST_INSERT_ID();





AttributeError: 'Connection' object has no attribute 'insert_id'
– Luca
Jun 30 at 9:06





Use connection.lastrowid, it is because you gave your cursor a connection name
– nacho
Jun 30 at 9:08





AttributeError: 'Connection' object has no attribute 'lastrowid' connection = self.engine.connect() ist the connector not a cursor. Or did I get this wrong?
– Luca
Jun 30 at 9:13





How it works? I make a INSERT and nothing will be returned. That's the next error: This result object does not return rows
– Luca
Jul 1 at 11:11






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux