stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.73k stars 1.57k forks source link

get data from SQLite and display in uitableview #963

Closed emraherten closed 3 years ago

emraherten commented 5 years ago

I'm using Swift 5 and SQLite. Database creation and table creation is done but I'm having trouble to get the data of the table and list this data in uitableview. I read all the documents but couldn't write the necessary code for that. Is anybody have any sample code to read data from SQLite and show in tableview? compatible for Swift 4 or Swift 5.

Jasperav commented 5 years ago

Same here. If this project supports something like nsfetchedresultscontroller I will switch right away.

jlawton commented 5 years ago

Since it seems to me that this project isn't intended to solve the problem of interacting with UIKit, this might not be the right place to ask, and I don't happen to have that use case.

The basic idea would be to read from the database using a Statement (such as db.prepare(users) in the documentation example) and possibly store that in an Array (Array(db.prepare(users))) to use in an implementation of UITableViewDataSource to configure some cells.

For larger data, implementing some paging would be a little more code. NSFetchedResultsController does a lot more than that, I think, and I don't know of an open library that implements this on top of SQLite.swift.

I'm sorry I don't have code to point at, or a more concrete answer, but an idea of where you are having a problem could help if you're still having trouble.

nathanfallet commented 3 years ago

In your table view controller viewDidLoad, fetch data from your database, and store it in an array. Then use this array to populate your table view.