第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

使用Python插入MySQL數(shù)據(jù)庫后,如何獲得“ id”?

使用Python插入MySQL數(shù)據(jù)庫后,如何獲得“ id”?

慕婉清6462132 2019-10-26 12:43:01
我執(zhí)行INSERT INTO語句cursor.execute("INSERT INTO mytable(height) VALUES(%s)",(height))我想獲取主鍵。我的表有2列:id      primary, auto incrementheight  this is the other column.我剛剛插入了這個(gè),如何獲得“ id”?
查看完整描述

3 回答

?
湖上湖

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個(gè)贊

另外,cursor.lastrowid(MySQLdb支持的dbapi / PEP249擴(kuò)展名):


>>> import MySQLdb

>>> connection = MySQLdb.connect(user='root')

>>> cursor = connection.cursor()

>>> cursor.execute('INSERT INTO sometable VALUES (...)')

1L

>>> connection.insert_id()

3L

>>> cursor.lastrowid

3L

>>> cursor.execute('SELECT last_insert_id()')

1L

>>> cursor.fetchone()

(3L,)

>>> cursor.execute('select @@identity')

1L

>>> cursor.fetchone()

(3L,)

cursor.lastrowidconnection.insert_id()比另一次MySQL往返便宜,而且便宜很多。


查看完整回答
反對 回復(fù) 2019-10-26
?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊

Python DBAPI規(guī)范還為光標(biāo)對象定義了“ lastrowid”屬性,因此...


id = cursor.lastrowid

...也應(yīng)該起作用,并且顯然基于每個(gè)連接。


查看完整回答
反對 回復(fù) 2019-10-26
  • 3 回答
  • 0 關(guān)注
  • 2130 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號