Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I've tried to create a Database using sqlite3.0 in my app, but the database hasn't created. Here is my code:

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

    docsDir = dirPaths[0];

    // Built the path to the database file
    _databasePath = [[NSString alloc]
                     initWithString: [docsDir stringByAppendingPathComponent:
                                      @"contacts1.db"]];//here the database name is contacts1.db.

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: _databasePath ] == NO)//checking Database exists
    {
        const char *dbpath = [_databasePath UTF8String];

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

            if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {
            }
            sqlite3_close(_contactDB);
        } 
    }

I don't what is the error?I've implemented the code in the ViewDidload()

share|improve this question
    
What is the open call returning. What is err!sg on return? What happens when you step through the code. A lot of basic information missing here. –  David Berry Jul 18 '14 at 13:41
    
Did you try setting break-points and tracing it step by step to see which lines aren't executed and what is possibly null? –  Neeku Jul 18 '14 at 13:42
1  
the sqlite3_exec should return an error - what is it ? –  Chris Jul 18 '14 at 13:42
    
it doesn't return anything, I've checked using break-points, it doesn't enter the if conditional statment block –  Gobinath Ed Jul 18 '14 at 13:50
    
@user Which if block exactly? –  Rohan Bhale Jul 18 '14 at 14:01

1 Answer 1

up vote 1 down vote accepted

try to check your condition like this

NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *doctumentsDirectory = [directories lastObject];
    self.databasePath = [[NSString alloc] initWithString:[doctumentsDirectory stringByAppendingPathComponent:@"/contacts1.db"]];

    NSFileManager *fileManager = [NSFileManager defaultManager];


    if (![filemgr fileExistsAtPath: _databasePath ])//checking Database exists
        {
            const char *dbpath = [_databasePath UTF8String];

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

                if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
                {
                }
                sqlite3_close(_contactDB);
            } 
        }
share|improve this answer
    
Super! It has been create with your effort. Thanks Guys –  Gobinath Ed Jul 18 '14 at 13:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.