Count of rows returned

I am following along the document "getting started with grab" and when I execute the following:

try dbQueue.read { db in
   let drafts = try Project.drafts().fetchAll(db)

Where can I find a count of the rows returned or how would I code a for loop to loop thru all the rows returned?

Thanks

Hello @Eagle442BR

Where can I find a count of the rows returned

Your drafts variable likely contains a Swift array. Like all Swift arrays, its number of elements is available with the count property:

try dbQueue.read { db in
   let drafts = try Project.drafts().fetchAll(db)
   let count = drafts.count

You are likely to want to extract this array from the read method:

let drafts = try dbQueue.read { db in
   try Project.drafts().fetchAll(db)
}
let count = drafts.count

You can also count database rows without fetching all of them with the fetchCount method:

let count = try dbQueue.read { db in
   try Project.drafts().fetchCount(db)
}

how would I code a for loop to loop thru all the rows returned?

Like all other Swift arrays, with a for loop:

let drafts = try dbQueue.read { db in
   try Project.drafts().fetchAll(db)
}
for draft in draft {
    print(draft)
}

I suggest you have another read of the following documentation chapters:

Thanks for the quick response... That got me going..I am new to swift as well as GRDB. I come from a "C" background and IBM 370/390 programming. So, I have a lot to learn, but looking forward in developing several applications.

I am sure I will have some more questions. :grinning:
Thanks again.

Sure, your questions are welcome. Please have a look at the documentation links, though!

I will and they have been very helpful!

Documentation is helpful, but somewhat too long. Just be patient, and stroll around :slight_smile:

Terms of Service

Privacy Policy

Cookie Policy