SQLite问题:sqlite3_open([databasePath UTF8String],& database)== SQLITE_OK)不工作

时间:2011-12-23 07:07:12

标签: iphone objective-c ios sqlite

-(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);

if()with comment不起作用....有些人有治疗方法!我已经上过3个小时....请拿出一个soln

2 个答案:

答案 0 :(得分:3)

您正在打开数据库两次。您必须关闭数据库连接,然后必须再次打开数据库。这就是为什么它不起作用。

答案 1 :(得分:0)

Hay你可以使用我的代码,它对我来说很好: -

 (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"];
}

如果需要澄清,请告诉我