0

Possible Duplicate:
In Python, after I INSERT Into mysqldb, how do I get the “id”?

How to fetch the primary key post insertion into foo, so that I can insert the foreign key reference in other tables.

import MySQLdb

cursor = mysqlconn.cursor()
cursor.execute("""INSERT INTO foo (value) VALUES (%s)""", (value))
prod_mysql_conn.commit()
Community
  • 1
  • 1
Joe
  • 13,573
  • 28
  • 78
  • 142

2 Answers2

1

As simple as:

mysqlconn.insert_id()

or

cursor.lastrowid
zerkms
  • 240,587
  • 65
  • 429
  • 525
0

like this:

  show index from table_name where Key_name = 'PRIMARY';

table_name is a table name. Have a look on the column_name field on the result, you will get that PRIMARY KEY column of the table.

Ravindra Bagale
  • 16,715
  • 9
  • 41
  • 70