Creating an empty field and getting the INTEGER primary key using sqlite, python

I am using sqlite with python. When I insert into table A, I need to pass it the identifier from table B. So what I would like to do is insert the default data into B, grab the identifier (which is an automatic increment) and use it in table A. What is the best way to get the key from the table I just pasted into?

+3
source share
3 answers

As the Christian said, sqlite3_last_insert_rowid()this is what you want ... but it is a C-level API, and you are using Python DB-API bindings for SQLite.

, lastrowid , ( "lastrowid" ), cursor.execute( ... ), - lastid = cursor.lastrowid, .

, "ID", , ... , ID? , B -, , , B.

+7

sqlite3_last_insert_rowid() - , , , :

SQLite 64- "rowid". ROWID, OID _ROWID_ as . table INTEGER PRIMARY KEY, rowid.

rowid INSERT . INSERT - , .

, ! ( ROWID .)

+1

:

SELECT last_insert_rowid();

, , , , .

+1

Source: https://habr.com/ru/post/1702672/


All Articles