I am inserting with twisted runQuery into mysql. How to i get the last inserted row id from the transaction? Actually i do another sql query after insert, but this isnt really elegant.
according to (Twisted adbapi: number affected rows and last insert id), there should be a lastrowid property but this seems not to be true.
@inlineCallbacks
def get_or_create_node_id(self, node_name):
try:
record_exists = yield self.db.runQuery("SELECT COUNT(*) FROM nodes WHERE name = %s", (node_name))
except ConnectionError:
log.error("db interaction failed due to connection error")
if record_exists[0]['COUNT(*)'] == 0:
log.info("creating new nodes entry {node_name}", node_name=node_name)
try:
call_insert = yield self.db.runQuery("INSERT INTO nodes (name) VALUES (%s)", (node_name))
node_id = yield self.db.runQuery("SELECT id FROM nodes WHERE name = %s", (node_name))
returnValue(int(node_id[0]['id']))
except ConnectionError:
log.error("db interaction failed due to connection error")
else:
try:
node_id = yield self.db.runQuery("SELECT id FROM nodes WHERE name = %s", (node_name))
returnValue(int(node_id[0]['id']))
except ConnectionError:
log.error("db interaction failed due to connection error")