NewDroidDev
NewDroidDev

Reputation: 1686

How to access sqlite file inside the xcode project?

I'm new to IOS development and I want to know how to access sqlite file inside xcode project. In other files they do this:

NSString *path = [mainBundle pathForResource: @"image" ofType: @"png"]; 

but when I'm trying this line of code it doesn't work in sqlite extension.

Upvotes: 0

Views: 184

Answers (1)

codercat
codercat

Reputation: 23301

ios_sqlite_database

ABSQLite - sample project

- (void)viewDidLoad {
        NSString *docsDir;
        NSArray *dirPaths;

        // Get the documents directory
        dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        docsDir = [dirPaths objectAtIndex:0];

        // Build the path to the database file
        databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];

        NSFileManager *filemgr = [NSFileManager defaultManager];

        if ([filemgr fileExistsAtPath: databasePath ] == NO)
        {
        const char *dbpath = [databasePath UTF8String];

                if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
                {
                        char *errMsg;
                        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";

                        if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
                        {
                                status.text = @"Failed to create table";
                        }

                        sqlite3_close(contactDB);

                } else {
                        status.text = @"Failed to open/create database";
                }
        }

        [filemgr release];
        [super viewDidLoad];
}

sqlite-tutorial-for-ios-making-our-app

Upvotes: 2

Related Questions