#!/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)
python插入记录后取得主键id的方法
最新推荐文章于 2023-07-17 15:12:09 发布