[Python] Sqlite. How to get value of Auto Increment Primary Key after Insert, other than...

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    I am using Sqlite3 with Flask microframework, but this question concerns only the Sqlite side of things..

    Here is a snippet of the code:

    g.db.execute('INSERT INTO downloads (name, owner, mimetype) VALUES (?, ?, ?)', [name, owner, mimetype])
    file_entry = query_db('SELECT last_insert_rowid()')
    g.db.commit()


    The downloads table has another column with the following attributes: id integer primary key autoincrement,

    If two people write at the same time the code above could produce errors.

    Transactions can be messy. In Sqlite is there a neat built in way of returning the primary key generated after doing an INSERT ?

    Continue reading...

Compartilhe esta Página