开发者

SQlite-FMDatabase query while loop not working

开发者 https://www.devze.com 2023-03-06 20:13 出处:网络
I have a table \"keywords\" that has 2 columns \"pk\" and \"text\". In firefox SQLite manager ,I can see that there are two names \"john\" and \"tom\" in \"text\" column. There is no error or warning

I have a table "keywords" that has 2 columns "pk" and "text". In firefox SQLite manager ,I can see that there are two names "john" and "tom" in "text" column. There is no error or warning in my code. When I am running it in simulator then I can see in console that I am never entering into While loop. it means FMResultSet "rs" is not getting any result from SQL query.I am getting "Opened successfully" message means my database was opened without any problem and database path, database name etc are correct. My query is also correct as it is not showing any query error in console. But i am not getting "while loop started" message. My database array is also empty so I know that while loop is not working. I am using FMDatabase. Here is my code

-(void) readWordsfromDatabase
{   
db=[FMDatabase databaseWithPath:globalDatabasePath];
globalDatabaseArray=[[NSMutableArray alloc] init];
[db setLogsErrors:TRUE ];
[db setTraceExecution:TRUE];
if (![db open])
{
    NSLog(@"Failed to open database");
    return;
}
else {
    NSLog(@"Opened successfully");
}
FMResultSet *rs= [db executeQuery:@"SELECT * FROM keywords"];
while([rs next])
{
    NSLog(@"while loop started");

    int aPK=[rs intForColumn:@"pk"];
    NSString *aText=[rs stringForColumn:@"text"];
    NSLog(@"aText is %@",aText);

    singleKeyword *sk=[[singleKeyword alloc] initWithData:aPK :aText];
    [globalDatabaseArray addObject:sk];
    [sk release];

            NSLog(@"text in array%@",[self.globalDatabaseArray  objectAtIndex:0]);  
    NSLog(@"text in array%@",[self.globalDatabaseArray  objectAtIndex:1]);
}//while closed
//NSLog(@"text in array%@",[self.globalDatabaseArray  objectAtIndex:0]);
    // NSLog(@"text in array%@",[self.globalDatabaseArray  objec开发者_如何学GotAtIndex:1]);   
[db close];
}


FMDB is opening the database no matter if the path to a file exists or not (actually FMDB is also designed that you could call [FMDatabase databaseWithPath:nil] without running into an error or warning). If you would like to check wether you are opening the 'right' database, you have to use the NSFileManager and validating the path.

- (BOOL)fileExistsAtPath:(NSString *)path

The most common mistake is that your sqlite-manager and your FMDB are referring to different databases.

0

精彩评论

暂无评论...
验证码 换一张
取 消