2011-12-28 33 views
1

我有一个项目,其中包括FMDB来管理SQLite数据库。我导入并链接了FMDB包装,但问题是当我查询数据库时没有显示结果:iPhone fdbm SQLite没有读取数据

用Firefox SQLite管理器(Ubuntu)创建的sqlite数据库,我将它复制到Xcode。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docsPath = [paths objectAtIndex:0]; 
NSString *path = [docsPath stringByAppendingPathComponent:@"db.sqlite"]; 

FMDatabase *db = [FMDatabase databaseWithPath:path]; 

[db open]; 

FMResultSet *fResult= [db executeQuery:@"SELECT * FROM mytable"]; 

while([fResult next]) 
{ 
    NSLog(@"%@",[fResult stringForColumn:@"title"]); 
} 
[db close]; 

回答

1

如果您复制数据库到Xcode中,那么你应该寻找在你的应用程序的主束资源路径先在数据库上,然后将其复制到文件目录,如果它不存在,那里还没有,只有你可以使用它。您可能需要使用lastErrorMessagelastErrorCode消息来调试FMDatabase对象。当然

FMDatabase *db = [FMDatabase databaseWithPath:path]; 
NSLog(@"database instantiated: %@", db]; 

[db open]; 

NSLog(@"Database has encountered an error with message: %@. And code: %d", db.lastErrorMessage, db.lastErrorCode]; 

FMResultSet *fResult= [db executeQuery:@"SELECT * FROM mytable"]; 

while([fResult next]) 
{ 
    NSLog(@"%@",[fResult stringForColumn:@"title"]); 
} 
[db close]; 

其他问题可能听起来很傻,但如果你的“MYTABLE”不包含什么,while循环表达永远是假的。但我最好的猜测 - 数据库不在Documents目录中。

+0

其实我在iphone上使用fdbm和资源文件夹中的数据库文件 – shox 2011-12-28 08:55:50

+0

如果数据库在资源文件夹中,那么在资源文件夹中寻找,而不是文件 - [[NSBundle mainBundle] pathForResource:@“db”ofType:@“源码“]; – Eugene 2011-12-28 08:57:33