1

一切对我来说都很好,但是 prepare_v2 没有得到 SQLITE_OK

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];

}

4

1 回答 1

0

我想通了,实际上我并没有将 sqlite 文件复制到 iPhone 项目中,现在运行良好

于 2012-06-28T00:46:00.220 回答