Deepukjayan
Deepukjayan

Reputation: 3695

SQLite issue: sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) not working

-(void)myDatabaseFunction
{   
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"HHAuditToolDatabase.sqlite"];
if (sqlite3_open([writableDBPath UTF8String], &database) == SQLITE_OK){
NSLog(@"opening db");
NSString *keyValue;
NSString *sqlStr = @"SELECT * FROM HHAuditTable";
//following if() not working dude!!!!
//its working with !=SQLITE_OK      
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

sqlite3_stmt *addStmt = nil;

if(sqlite3_prepare_v2(database,[sqlStr UTF8String], -1, &addStmt, NULL) != SQLITE_OK){
    NSLog(@"%@",sqlStr);
    while (sqlite3_step(addStmt) == SQLITE_ROW) {


const unsigned char *querry_returns = sqlite3_column_text(addStmt, 0);
        keyValue = [[NSString alloc]initWithUTF8String:(char *) sqlite3_column_text(addStmt, 0)];
        }

        NSLog(@"value from DB = %@",keyValue);

That if() with comment doesn't work....Some have a cure!!! i have been on it for last 3 hrs....please come up with a soln

Upvotes: 0

Views: 2419

Answers (2)

Hardik Shah
Hardik Shah

Reputation: 1693

You are opening the database twice. You should have to close the database connection and then you have to open the database again. That is why it is not working.

Upvotes: 3

sandy
sandy

Reputation: 1948

Hay you can use my code its working fine for me:-

 (void)myDatabaseFunction {

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath]; 

if(!success) {

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"flipsy.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (!success) 
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);

}
NSString *sql = @"SELECT * FROM HHAuditTable";
 NSInteger *intvalue;
    result = [[NSMutableArray alloc] init]; 
   // NSLog(@"sql--------->%@",sql);
    const char *sqlStatement = [sql UTF8String];

//if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    if(sqlite3_prepare_v2(database, sqlStatement, -1, &selectstmt , NULL)!= SQLITE_OK) {
    }

    else {

        //NSLog(@"%@",dataTypeArray);

        for(int i=0;i<[dataTypeArray count];i++) {
            temp = [[NSMutableArray alloc] init];
            [result addObject:temp];
           // [temp release];
        }
        while(sqlite3_step(selectstmt) == SQLITE_ROW) {

            for(int i=0;i<[dataTypeArray count];i++) {

                switch( [[dataTypeArray objectAtIndex:i] integerValue] ) {

                    case 0:
                        intvalue = (NSInteger *)sqlite3_column_int(selectstmt,i);
                        strvalue = [NSString stringWithFormat:@"%d",intvalue];
                        [[result objectAtIndex:i] addObject:(NSNumber *)strvalue];


                        break;

                    case 1:
                        [[result objectAtIndex:i]  addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, i)]];
                        break;

                    case 2:

                        blob = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstmt, i) length:sqlite3_column_bytes(selectstmt, i)];
                        [[result objectAtIndex:i] addObject:blob];
                        [blob autorelease];
                        break;


                    default:
                        defaultValue=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, i)];
                        break;

                    }//switch

            }//for(int i=0;i<[dataTypeArray count];i++)

        }//while(sqlite3_step(selectstmt) == SQLITE_ROW)

    //}//else

    //sqlite3_close(database);
    sqlite3_finalize(selectstmt);
    [temp release];

}//if (sqlite3_open([dbPath UTF

}

- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"flipsy.sqlite"];
}

Please let me know if any clarification needed

Upvotes: 0

Related Questions