Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5975739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:07:43+00:00 2026-05-22T21:07:43+00:00

How to retrieve inserted id after inserting row in SQLite using Python? I have

  • 0

How to retrieve inserted id after inserting row in SQLite using Python? I have table like this:

id INT AUTOINCREMENT PRIMARY KEY,
username VARCHAR(50),
password VARCHAR(50)

I insert a new row with example data username="test" and password="test". How do I retrieve the generated id in a transaction safe way? This is for a website solution, where two people may be inserting data at the same time. I know I can get the last read row, but I don’t think that is transaction safe. Can somebody give me some advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:07:44+00:00Added an answer on May 22, 2026 at 9:07 pm

    You could use cursor.lastrowid (see "Optional DB API Extensions"):

    connection=sqlite3.connect(':memory:')
    cursor=connection.cursor()
    cursor.execute('''CREATE TABLE foo (id integer primary key autoincrement ,
                                        username varchar(50),
                                        password varchar(50))''')
    cursor.execute('INSERT INTO foo (username,password) VALUES (?,?)',
                   ('test','test'))
    print(cursor.lastrowid)
    # 1
    

    If two people are inserting at the same time, as long as they are using different cursors, cursor.lastrowid will return the id for the last row that cursor inserted:

    cursor.execute('INSERT INTO foo (username,password) VALUES (?,?)',
                   ('blah','blah'))
    
    cursor2=connection.cursor()
    cursor2.execute('INSERT INTO foo (username,password) VALUES (?,?)',
                   ('blah','blah'))
    
    print(cursor2.lastrowid)        
    # 3
    print(cursor.lastrowid)
    # 2
    
    cursor.execute('INSERT INTO foo (id,username,password) VALUES (?,?,?)',
                   (100,'blah','blah'))
    print(cursor.lastrowid)
    # 100
    

    Note that lastrowid returns None when you insert more than one row at a time with executemany:

    cursor.executemany('INSERT INTO foo (username,password) VALUES (?,?)',
                   (('baz','bar'),('bing','bop')))
    print(cursor.lastrowid)
    # None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to retrieve the primary key of an inserted row,
I need to get the last inserted id of table that have multi-column primary
I was using the following code to retrieve HTML snippets from a database table
I'm trying to retrieve the id of the last record inserted in the table
Does ruby support a way to retrieve the id for the last inserted row
I've read this question about getting the identity of an inserted row. My question
I want retrieve the id of a inserted row in the database, but I
I want to retrieve the last inserted _id , using mongoose as MongoDB wrapper
I'm using before and after insert triggers to generate ids (primary key) of the
I'm using this example to retrieve database results with ajax, and its working great.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.