Skip to content

Commit 51ec573

Browse files
authored
Update README.md
It took me some digging to figure out how to get the primary key of an inserted row. This might be helpful for some folks.
1 parent 548bc60 commit 51ec573

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ async function main() {
168168
main();
169169
```
170170

171+
#### Getting the ID of the Row You Inserted
172+
173+
```js
174+
const sqlite = require('sqlite');
175+
const SQL = require('sql-template-strings');
176+
177+
async function main() {
178+
const db = await sqlite.open("./my-db.sqlite");
179+
const firstName = 'Tamika';
180+
const lastName = 'Washington';
181+
// First value is auto-incrementing primary key
182+
const statement = await db.run(SQL`insert into People values (NULL, ${firstName}, ${lastName})`);
183+
// Get access to the primary key of the inserted row:
184+
console.log(statement.lastID);
185+
await sqlite.close(db);
186+
}
187+
188+
main();
189+
```
190+
171191
### References
172192

173193
* [Using SQLite with Node.js for Rapid Prototyping](https://medium.com/@tarkus/node-js-and-sqlite-for-rapid-prototyping-bc9cf1f26f10) on Medium.com

0 commit comments

Comments
 (0)