# GRDB inserted row is not accessible

**URL:** <https://forums.swift.org/t/grdb-inserted-row-is-not-accessible/73720>\
**Category:** GRDB\
**Created:** [August 6, 2024, 12:46am UTC](https://forums.swift.org/t/grdb-inserted-row-is-not-accessible/73720 "2024-08-06T00:46:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![warrenpettit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/warrenpettit/32/36779_2.png) [@warrenpettit](https://forums.swift.org/u/warrenpettit)\
**Post date:** [August 6, 2024, 12:46am UTC](https://forums.swift.org/t/grdb-inserted-row-is-not-accessible/73720/1 "2024-08-06T00:46:21Z")

</div>

I have a table defined as:

try db.create(table: "availablePlayer") {table in  
table.autoIncrementedPrimaryKey("playerId")  
table.column("name"), .text).notNull()

In my testing, I am selecting 9 rows from one table and inserting those rows into this "availablePlayer" table. This works. When I query the "availablePlayer" table, I have playerId's from 301 to 309. I then attempt to add a new player to the "availablePlayer" table. The trace show this statement as

INSERT INTO availablePlayer (name) VALUES('Smith').

After the insert, I execute

let lastRow = db.lastInsertedRowID

which returns a value of 310. This all makes sense. But when I use DB Browser for SQLite to view the rows in the "availablePlayer" table, there is no row with a playerId of 310 and a name of "Smith". Also, when I query sqlite\_sequence, it show the seq column as having a value of 310.  
If I then delete all of the rows in the "availablePlayer" table and rerun my app, the 9 rows get inserted. And the rows have playerId's of 311 to 318

I am not getting any errors.

As a follow up. I commented out the code where I deleted the existing rows in "availablePlayer" and then populate it. So now when I added a new player, the table was empty. The new player was added successfully. I have move my delete into its own {db in ...} and instead of doing all of my inserts in a loop within a single {db in ...} structure, I have moved the loop outside of the {db in ... }. But none of this has had a positive affect on the outcome. I just am at a loss.

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [August 6, 2024, 2:57am UTC](https://forums.swift.org/t/grdb-inserted-row-is-not-accessible/73720/2 "2024-08-06T02:57:23Z")

</div>

Not familiar with `GRDB`, but could it be that some kind of `flush-to-db` op is needed for the data to be eventually written to the permanent store?

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [August 6, 2024, 5:14am UTC](https://forums.swift.org/t/grdb-inserted-row-is-not-accessible/73720/3 "2024-08-06T05:14:21Z")

</div>

Hello @warrenpettit,

As suggested above, make sure your changes are persisted on disk by proceeding to the end of the database transaction:

```swift
try dbQueue.write { db in
  // Inserts, deletes and updates
  // are NOT committed to disk...
}
// <- until the transaction is completed. 

```

Take also care of not deleting the inserted rows before checking your assumptions in DB Browser for SQLite. You'll see exactly what your app is doing by enabling the [SQL trace](https://swiftpackageindex.com/groue/grdb.swift/documentation/grdb/configuration#Tracing-SQL-Statements).

---

<div class="post-metadata">

**Author:** ![warrenpettit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/warrenpettit/32/36779_2.png) [@warrenpettit](https://forums.swift.org/u/warrenpettit)\
**Post date:** [August 6, 2024, 1:26pm UTC](https://forums.swift.org/t/grdb-inserted-row-is-not-accessible/73720/4 "2024-08-06T13:26:03Z")

</div>

I am trying to learn SwiftUI and I am having a hard time with when SwiftUI fires off events. A that was the problem here. The code that cleared my table and repopulated it was executed. Then later, I executed the code to add the new player. But as it turns out, the clearing the table and the repopulation was fired a second time. Hence, the added player doesn't show up. So it had nothing to do with GRDB.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [August 6, 2024, 4:42pm UTC](https://forums.swift.org/t/grdb-inserted-row-is-not-accessible/73720/5 "2024-08-06T16:42:30Z")

</div>

I'm glad you could find the origin of the issue. Don't hesitate using the debugging tools, such as tracing SQL statements: they can answer many questions. There is also a FAQ in the main README that can help when troubleshooting. In all cases, it's always ok to ask questions 👍
