我有一个项目,其中我存储了sqlite数据库文件“data.sqlite3” '分组' &安培;文件 ' - ' 资源'
以下是我的viewcontroller源代码
//-myviewcontroller.h
#import "sqlite3.h"
#define kFilename @"data.sqlite3"
//myviewcontroller.m
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
-(void)f
{
if (sqlite3_open([[self dataFilePath] UTF8String],&database)!=SQLITE_OK)
//dataFilePath returns
///Users/interdev/Library/Application Support/iPhone Simulator/User/Applications/095C6E05-4EAE-4817-883E-A72E39D439E0/Documents/data.sqlite3
{
sqlite3_close(database);
NSAssert(0,@"Failed to open database");//no problem
}
NSString *query = @"SELECT * FROM table1 ORDER BY ROW";//table1 is table name
sqlite3_stmt *statement;
NSInteger v=sqlite3_prepare_v2( database, [query UTF8String],
-1, &statement, nil);
NSString *zs= [NSString stringWithFormat:@"%d",v];
NSLog(@" The buttontitile is %@ ",zs);
if ( v == SQLITE_OK) { // ...
}
我在日志中检查了v的值,它总是1
#define SQLITE_ERROR 1 /* SQL error or missing database */
我不知道为什么会这样。
答案 0 :(得分:1)
看起来您的代码正在您的应用程序的“Documents”文件夹中查找您的数据库。
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
因此,如果data.sqlite3文件位于Resources文件夹中,则应用程序将不会找到该数据库。最好在应用程序的“Documents”文件夹中创建和存储sqlitedatabase。例如/ Users / interdev / Library / Application Support / iPhone Simulator / User / Applications / 095C6E05-4EAE-4817-883E-A72E39D439E0 / Documents / data.sqlite3