1
一切看起來都對我好,但prepare_v2沒有得到SQLITE_OK想不通,爲什麼沒有被執行插入SQLite的SQL語句時,Xcode
的SQL指令是非常基本的,如果我複製並粘貼上sqlite管理器,它插入罰款
有什麼我失蹤?
//from view controller
- (void)viewDidLoad
{
[super viewDidLoad];
DBConnection* dbCon = [DBConnection alloc];
[dbCon init];
[dbCon InsertDataRollNo:@"888"];
}
-(id)init
{
//[super init];
DBName = @"FoodDB2.sqlite";
NSArray* documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDir = [documentsPath objectAtIndex:0];
DBPath = [documentsDir stringByAppendingPathComponent:DBName];
return self;
}
-(void)InsertDataRollNo:(NSString*)theName
{
[self CheckAndCreateDB];
sqlite3* database;
if(sqlite3_open([DBPath UTF8String], &database)== SQLITE_OK)
{
NSString* statement;
sqlite3_stmt* compliedStatement;
//statement = [[NSString alloc]initWithFormat:@"insert into Category(TheName)values('%d')",@"epa"];
statement = @"insert into Category(TheName)values('aaa')";
const char* sqlStatement = [statement UTF8String];
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compliedStatement, NULL) == SQLITE_OK)
{
if(SQLITE_DONE!=sqlite3_step(compliedStatement))
{
NSAssert1(0,@"Error by inserting '%s' ", sqlite3_errmsg(database));
}
else
{
NSAssert1(0,@"cool '%s' ", @"ope");
}
}
sqlite3_finalize(compliedStatement);
}
sqlite3_close(database);
}
-(void)CheckAndCreateDB
{
BOOL success;
NSFileManager* fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:DBPath];
if(success) return;
NSString* databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName];
[fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil];
//[fileManager release];
}
請確保路徑是正確的,並且您有寫入文件的權限。還是行不通? –
路徑是正確的,你是什麼意思文件的權限? mac文件系統寫入權限?我向每個人添加了對sqlite文件的文件系統寫權限,但仍然失敗 – RollRoll
你可以發佈你的'-CheckAndCreateDB'嗎? –