为什么我不能执行这段代码?

时间:2009-05-14 09:22:09

标签: iphone sqlite

NSString *event=@"max";
NSString *venue=@"tvm";
NSString *edate=@"may 6";
NSString *etime=@"10:30";
int admts=5;
NSString *ima=ticobj.iimg;
sqlite3 *database;
databaseName = @"smbhDB.sql";
sqlite3_stmt *addStatement ;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
if(addStatement == nil) {
    const char *sql ="insert into tickets (admittance, venue, event, date, time, imagedata) Values (?,?,?,?,?,?)";

    if(sqlite3_prepare_v2(database, sql, -1, &addStatement, NULL) != SQLITE_OK)
        NSAssert1(0, @"444 Error while creating add  statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_int(addStatement,1,admts);

sqlite3_bind_text(addStatement, 2, [venue UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 3, [event UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 4, [edate UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 5, [etime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStatement, 6, [ima UTF8String], -1, SQLITE_TRANSIENT);

if(SQLITE_DONE != sqlite3_step(addStatement))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
if(sqlite3_prepare_v2(database, sql, -1, &Statement, NULL) == SQLITE_OK) {
sqlite3_exec(database, sql, -1, &Statement, NULL);
}

sqlite3_reset(addStatement);

为什么我不能执行此代码?

2 个答案:

答案 0 :(得分:4)

在哪里

sqlite3_open([databasePath UTF8String], &database);

您无法使用已关闭的数据库。

答案 1 :(得分:0)

-(BOOL)insertDatabaseValue:(SignIn *)objFavourie isUpdate:(BOOL)update
{

BOOL returnValue = YES;
sqlite3_stmt *insertStmt = nil;
sqlite3  *database = nil;

if (sqlite3_config(SQLITE_CONFIG_SERIALIZED) == SQLITE_OK){
    returnValue = NO;
}



if (sqlite3_open([DataBasePath UTF8String], &database) == SQLITE_OK){



    if(insertStmt == nil) {
        NSString *aString;
        if(!update)
            aString =[NSString stringWithFormat:@"INSERT INTO Sanjay('Name', 'Password') Values(?, ?)"];

        else 
            aString =[NSString stringWithFormat:@"UPDATE Sanjay SET Password = ? where 'Name'=%@",objFavourie.strName]; 


        const char *sql = [aString UTF8String];

        if(sqlite3_prepare_v2(database, sql, -1, &insertStmt, NULL) != SQLITE_OK)
        {
            NSLog(@"Error while creating insert :%s add statement.", sqlite3_errmsg(database));
            returnValue = NO;
        }
    }
    if(sqlite3_bind_text(insertStmt, 1, [objFavourie.strName UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK){
        returnValue = NO;
    }
    if(sqlite3_bind_text(insertStmt, 2, [objFavourie.strPassword UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK) {
        returnValue = NO;
    }

    if(SQLITE_DONE != sqlite3_step(insertStmt)){
        NSLog(@"Error while inserting into '%s'", sqlite3_errmsg(database)); 
        returnValue = NO;
    }
    sqlite3_reset(insertStmt);    


    if (insertStmt) {
        sqlite3_finalize(insertStmt);
        insertStmt = nil;
    }

    sqlite3_close(database);
    database = nil;
}
else {
    returnValue = NO;
}

return returnValue; 

}