SQlite-FMDatabase 查询 while 循环不起作用
我有一个表“关键字”,其中有 2 列“pk”和“文本”。在firefox SQLite管理器中,我可以看到“text”列中有两个名字“john”和“tom”。我的代码中没有错误或警告。当我在模拟器中运行它时,我可以在控制台中看到我从未进入 While 循环。这意味着 FMResultSet“rs” 没有从 SQL 查询中获得任何结果。我收到“打开成功”消息意味着我的数据库已打开,没有任何问题,并且数据库路径、数据库名称等都是正确的。我的查询也是正确的,因为它在控制台中没有显示任何查询错误。但我没有收到“while 循环开始”消息。我的数据库数组也是空的,所以我知道 while 循环不起作用。我正在使用 FMDatabase。这是我的代码
-(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 objectAtIndex:1]);
[db close];
}
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 objectAtIndex:1]);
[db close];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论文件路径是否存在,FMDB 都会打开数据库(实际上 FMDB 还设计为您可以调用
[FMDatabase databaseWithPath:nil]
而不会遇到错误或警告)。如果您想检查是否打开“正确”的数据库,则必须使用 NSFileManager 并验证路径。最常见的错误是您的 sqlite-manager 和 FMDB 引用了不同的数据库。
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.The most common mistake is that your sqlite-manager and your FMDB are referring to different databases.