Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5009135
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2015-07-06 14:28:39


  1. #!/usr/bin/python
  2. # import MySQL module
  3. import MySQLdb
  4. # get user input
  5. name = raw_input("Please enter a name: ")
  6. # connect
  7. conn = MySQLdb.connect(host="localhost", user="nobody", passwd="nobody", conn="qestar", unix_socket="/tmp/mysql.sock")
  8. # create a cursor
  9. cursor = conn.cursor()
  10. # execute SQL statement
  11. cursor.execute("INSERT INTO test (nama) VALUES (%s)", name)
  12. # get ID of last inserted record
  13. print "ID of last record is ", int(cursor.lastrowid) #最后插入行的主键ID
  14. print "ID of inserted record is ", int(conn.insert_id()) #最新插入行的主键ID,conn.insert_id()一定要在conn.commit()之前,否则会返回0
  15. conn.commit()
cursor.lastrowid跟conn.insert_id()的结果一般情况下是一样的,最后一条记录肯定就是刚刚插入的记录。但如果是并发插入就不一样了,多线程的时候


阅读(2930) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~