参考:https://blog.csdn.net/qq_37788558/article/details/78151972

python插入记录后获取最后一条数据的id

#!/usr/bin/python  
# import MySQL module  
import MySQLdb  
# get user input  
name = raw_input("Please enter a name: ")  
# connect  
conn = MySQLdb.connect(host="localhost", user="nobody", passwd="nobody", conn="qestar", unix_socket="/tmp/mysql.sock")  
# create a cursor  
cursor = conn.cursor()  
# execute SQL statement  
cursor.execute("INSERT INTO test (nama) VALUES (%s)", name)  
# get ID of last inserted record  
print "ID of last record is ", int(cursor.lastrowid) #最后插入行的主键ID  
print "ID of inserted record is ", int(conn.insert_id()) #最新插入行的主键ID,conn.insert_id()一定要在conn.commit()之前,否则会返回0  
conn.commit()  

  

 

相关文章:

  • 2022-12-23
  • 2021-12-08
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2021-08-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2021-07-14
相关资源
相似解决方案