SQlite-FMDatabase 查询 while 循环不起作用

发布于 2024-11-08 08:07:18 字数 1375 浏览 0 评论 0原文

我有一个表“关键字”,其中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2024-11-15 08:07:41

无论文件路径是否存在,FMDB 都会打开数据库(实际上 FMDB 还设计为您可以调用 [FMDatabase databaseWithPath:nil] 而不会遇到错误或警告)。如果您想检查是否打开“正确”的数据库,则必须使用 NSFileManager 并验证路径。

- (BOOL)fileExistsAtPath:(NSString *)path

最常见的错误是您的 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.

- (BOOL)fileExistsAtPath:(NSString *)path

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文