How to retrieve data from SQLite database in iOS

Hello pals 🙂 Let’s start the day with the third post of this iOS database series, which is about getting back the data that you have saved in previous lesson. Up to now you are aware with  the way of creating database and inserting data to it. 

Just like in the Android example, we will get a similar interface after completing this lesson.

Figure One

Figure 1

What I’m going to do here is, get back all the data I have saved in my SQLite database and display only the name of the undergraduate in my UITableView. You can remember that our undergraduates have three attributes including name. Other two attributes are undergraduate’s University ID and his/her GPA. Therefor when I retrieve data from database, I make sure get all those relevant data and assign them to a UndergraduateObj class which has above mentioned three attributes. Simply call, applying object oriented programming 😉

Without lengthy description I would like you to show the retrieving code part.

- (NSArray *)retrieveAllUndergrads {
 if (debugEnable) NSLog(@"%s - %d", __PRETTY_FUNCTION__, __LINE__);
 const char *utf8Dbpath = [databasePath UTF8String];
 if (sqlite3_open(utf8Dbpath, &database) == SQLITE_OK) {
 NSString *querySQL = @"select * from undergraduate_details_table";
 const char *utf8QuerySQL = [querySQL UTF8String];
 if (sqlite3_prepare_v2(database, utf8QuerySQL, -1, &statement, NULL) == SQLITE_OK) {
 NSMutableArray *personArr = [[NSMutableArray alloc] init];
 while (sqlite3_step(statement) == SQLITE_ROW) {
 int totalColumns = sqlite3_column_count(statement);
 UndergraduateObj *undergrad = [UndergraduateObj alloc];
 NSString *uName;
 NSString *uUniId;
 NSString *uGpa;
 for (int i=0; i<totalColumns; i++){
 char *dbDataAsChars = (char *)sqlite3_column_text(statement, i);

 if (dbDataAsChars != NULL) {
 switch (i) {
 case 0:
 uName = [NSString stringWithUTF8String:dbDataAsChars];
 case 1:
 uUniId = [NSString stringWithUTF8String:dbDataAsChars];
 case 2:
 uGpa = [NSString stringWithUTF8String:dbDataAsChars];
 undergrad.undergradName = uName;
 undergrad.undergradUniId = uUniId;
 undergrad.undergradGpa = uGpa;
 [personArr addObject:undergrad];
 return [personArr copy];
 } else {
 if (debugEnable) NSLog(@"%s - %d # sqlite3_prepare_v2 is NOT ok", __PRETTY_FUNCTION__, __LINE__);
 } else {
 if (debugEnable) NSLog(@"%s - %d # Fail to open DB", __PRETTY_FUNCTION__, __LINE__);
 return nil;
 return nil;

I don’t know what really happened to my code formatting when I copy it to WordPress. Really sorry about missing the indentations. I tried to manually add them but then it get messy. What you can do is copy this code in to your Xcode and read it. Sorry again 😦

Okay… that’s all about for this post. You can get a descriptive idea about the SQLite functions by going to the link that I have provided with the code comments. Full source code I’ll upload to GitHub in the next tutorial which will be going to our last for this database series. 🙂 Till then have a safe and sound coding.

About AnujAroshA

Working as an Associate Technical Lead. Specialized in iOS application development. A simple person :)
This entry was posted in iOS, MySQL and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s