加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python插入数据表获取自增id

(2016-04-07 16:10:07)
标签:

it

分类: python
  1. #!/usr/bin/python  
  2. import MySQL module  
  3. import MySQLdb  
  4. get user input  
  5. name raw_input("Please enter name: " 
  6. connect  
  7. conn MySQLdb.connect(host="localhost"user="nobody"passwd="nobody"conn="qestar"unix_socket="/tmp/mysql.sock" 
  8. create 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()

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有