#!/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()
# 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()
cursor.lastrowid跟conn.insert_id()的结果一般情况下是一样的,最后一条记录肯定就是刚刚插入的记录。但如果是并发插入就不一样了,多线程的时候
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。