您的位置:首页 > 编程语言 > Python开发

python插入记录后取得主键id的方法

2012-04-04 22:30 323 查看
#!/usr/bin/python

# import MySQL module

import MySQLdb

# get user input

name = raw_input("Please enter a name: ")

# connect

db = MySQLdb.connect(host="localhost", user="nobody", passwd="nobody", db="qestar", unix_socket="/tmp/mysql.sock")

# create a cursor

cursor = db.cursor()

# execute SQL statement

cursor.execute("INSERT INTO test (nama) VALUES (%s)", name)

# get ID of last inserted record

print "ID of inserted record is ", int(cursor.lastrowid)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: