Python MySQL 插入后的主键ID
2016-05-05 16:12
429 查看
cursor.lastrowid
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()
相关文章推荐
- 利用pyhton 生成PDF文件
- Python +字符串+列表+字典
- Python3使用requests模块显示下载进度
- 在python下配置numpy,scipy
- vim升级7.4(启用python支持+遇到的错误)
- Python 32位 or 64位
- 中文字符乱码问题简单解决 python
- python使用mongo数据库
- UDPClient.py&UDPServer.py——我的第一行Python代码
- mininet python api
- Jupyter(IPython)
- 191. Number of 1 Bits [easy] (Python)
- PyCharm添加Python自定义模板
- 用Python制作简单的爬虫---爬虫基本思想
- 231. Power of Two [easy] (Python)
- python的学习笔记
- Python处理Excel(三):内置数据类型处理Excel数据
- python 中的赋值、引用、拷贝、作用域
- python第二站续--字典dict
- python代码结构