Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the primary key back from a SQL insert with SQLite

Tags:

sql

sqlite

I have a SQL table set that looks like this

create table foo ( 
    id int primary key asc, 
    data datatype );

create table bar ( 
    id int primary key asc, 
    fk_foo int,
    foreign key(foo_int) references foo(id));

Now, I want to insert a record set.

insert into table foo (data) values (stuff);

But wait - to get Bar all patched up hunkydory I need the PK from Foo. I know this is a solved problem.

What's the solution?

like image 377
Paul Nathan Avatar asked Oct 27 '25 04:10

Paul Nathan


1 Answers

try this

SELECT last_insert_rowid() 
like image 73
SysAdmin Avatar answered Oct 29 '25 19:10

SysAdmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!