How to insert data into SQLite database in iOS

Hi everybody, it’s a sunny Sunday here 🙂 Let’s continue our SQLite database series. Today I’m going to write the second post of this series which is, insert data to a SQLite database in iOS. You can learn how to create a database with my earlier post. For the simplicity I’m writing these basic database related functions in separate blog posts. 

Following is my interface where you will be able to give inputs and save it in the database that we have already created.

Figure One

Figure 1

Just as I told you in the earlier post, I’m going to use the same concept that we have used in SQLite with Android blog post. When you press the Save button what happen is, it will collect data inputs from above three text fields and pass it to execute the insert query. Below is the code for save action.

- (IBAction)saveAction:(UIButton *)sender {
 if (debugEnable) NSLog(@"%s - %d", __PRETTY_FUNCTION__, __LINE__);
 NSString *undGradName = _nameTxtField.text;
 NSString *undGradUniId = _uniIdTxtField.text;
 NSString *undGradGpa = _gpaTxtField.text;
 BOOL isSuccess = [[SQLiteDBManager sharedInstance] insertName:undGradName uniId:undGradUniId gpa:undGradGpa];
 if (isSuccess) {
 if (debugEnable) NSLog(@"%s - %d # success", __PRETTY_FUNCTION__, __LINE__);
 _nameTxtField.text = @"";
 _uniIdTxtField.text = @"";
 _gpaTxtField.text = @"";
 [_nameTxtField becomeFirstResponder];
 } else {
 if (debugEnable) NSLog(@"%s - %d # fail", __PRETTY_FUNCTION__, __LINE__);

Hope you can remember that we implemented singleton database instance. Therefore we are not allocate memory or initiate it again unless that database instance is nil. Below is the code where you can find all SQLite related functions and queries for data insertion.

- (BOOL)insertName:(NSString *)name uniId:(NSString *)uniId gpa:(NSString *)gpa {
 if (debugEnable) NSLog(@"%s - %d", __PRETTY_FUNCTION__, __LINE__);
 const char *utf8Dbpath = [databasePath UTF8String];
 if (sqlite3_open(utf8Dbpath, &database) == SQLITE_OK) {
 NSString *insertQuery = [NSString stringWithFormat:@"insert into undergraduate_details_table (undergraduate_name_column, undergraduate_uni_id_column, undergraduate_gpa_column) values (\"%@\", \"%@\", \"%f\")", name, uniId, [gpa doubleValue]];
 const char *utf8InsertQuery = [insertQuery UTF8String];
 sqlite3_prepare_v2(database, utf8InsertQuery, -1, &statement, NULL);
 if (sqlite3_step(statement) == SQLITE_DONE) {
 return YES;
 } else {
 return NO;
 } else {
 if (debugEnable) NSLog(@"%s - %d # Fail to open DB", __PRETTY_FUNCTION__, __LINE__);
 return NO;

Again, I’m not going to explain SQLite functions, instead I put a URL inside comment line above those functions which direct you to the documentation.

Now you are familiar with SQLiteBrowser application which, you can import the database file and see what really happened to it in a GUI mode. Therefore I think this is a good place to end this blog post. Quick remind; you will get the source code at the end of this series. See you again with the next post of this series 😉


About AnujAroshA

Working as a Senior Software Engineer. Specialized in iOS application development. A simple person :)
This entry was posted in iOS, MySQL and tagged , , . Bookmark the permalink.

One Response to How to insert data into SQLite database in iOS

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s