Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sqlite.md with instructions on fetching last inserted row ID #10932

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/api/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ query.get({ $message: "Hello world" });

Internally, this calls [`sqlite3_reset`](https://www.sqlite.org/capi3ref.html#sqlite3_reset) followed by [`sqlite3_step`](https://www.sqlite.org/capi3ref.html#sqlite3_step) until it no longer returns `SQLITE_ROW`. If the query returns no rows, `undefined` is returned.

Use the "RETURNING" SQL statement and `get()` when inserting a record to grab the last inserted row id.

```ts
const insertStatement = "INSERT INTO foo (greeting) VALUES ($greeting) RETURNING id;"
const insertQuery = db.query(insertStatement, { $greeting: "Welcome to Bun!" });
console.log(await insertQuery.get());
// Output: { id: 1 }
```

### `.run()`

Use `.run()` to run a query and get back `undefined`. This is useful for schema-modifying queries (e.g. `CREATE TABLE`) or bulk write operations.
Expand Down