Jump to content
How To Create a SQLite CRUD Mobile App | iOS 5 | iPhone | iPad
Submitted by kevlangdo
Posted Jul 05 2012 01:52 AM
SQLite offers a very easy development model for building iOS apps with a storage facility. In addition the Cocoa Touch SDK along with Xcode offer a fantastic development environment for RAD application development.
The SQLite API can create a database on the fly using the open command if no database is present by getting the path to the document directory as the following code demonstrates assuming that the db variable is already defined.
NSArray * dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//Define new path for database
NSString * documentPath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"taskDb.db"];
fileExist = [[NSFileManager alloc] fileExistsAtPath:documentPath];
if(!(sqlite3_open([documentPath UTF8String], &db) == SQLITE_OK))
//define the tables
Likewise you can create tables or access the existing tables using the following code:
const char *sqlTable = "create table if not exists todoTbl(todoName varchar, todoDescription varchar, todoDate varchar)";
if(sqlite3_exec(db, sqlTable, NULL, NULL, &emsg) != SQLITE_OK)
NSLog(@"There is a problem with statement");
This code works best in the AppDelegate implementation file.
For a full tutorial on how to create and use the SQLite database to create an application that uses the SQLite API take a look at the following link:
Creating a CRUD SQLite application for iOS devices