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
  • 14,513
  • 28
  • 82
  • 144

2 Answers2

1

As simple as:

mysqlconn.insert_id()

or

cursor.lastrowid
zerkms
  • 249,484
  • 69
  • 436
  • 539
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
  • 17,226
  • 9
  • 43
  • 70