目标c中的SQLite内存不足错误

时间:2011-10-22 07:37:31

标签: objective-c ios xcode sqlite

我找到了一个关于带有目标c的sqlite的例子,并使用了我的项目。但我有一个问题。在创建add语句时。错误是“内存不足”的例外。我该如何修复此错误?

在viewcontroller类中:

iDailyAppDelegate *appDelegate = (iDailyAppDelegate *)[[UIApplication sharedApplication] delegate];

//Create a Coffee Object.
MyClass *coffeeObj = [[MyClass alloc] initWithPrimaryKey:0];
coffeeObj.Date = dateInString;
NSDecimalNumber *temp = [[NSDecimalNumber alloc] initWithString:first];
coffeeObj.Latitude = temp;
NSDecimalNumber *temp2 = [[NSDecimalNumber alloc] initWithString:second];
coffeeObj.Longitude = temp2;
[temp release];
coffeeObj.isDirty = NO;
coffeeObj.isDetailViewHydrated = YES;

//Add the object
[appDelegate addCoffee:coffeeObj];

sql add方法:

- (void) addCoffee {

if(addStmt == nil) {
    const char *sql = "insert into records(Date, Latitude, Longitude) Values(?, ?, ?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(addStmt, 1, [Date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(addStmt, 2, [Latitude doubleValue] );
sqlite3_bind_double(addStmt, 3, [Longitude doubleValue] );

if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
    RecordID = sqlite3_last_insert_rowid(database);

//Reset the add statement.
sqlite3_reset(addStmt);
}

1 个答案:

答案 0 :(得分:0)

在我的情况下,我复制并粘贴了一些代码,我在关闭数据库连接后尝试执行SQL语句。我删除了错误的行:

success = [dbHandler executeUpdate:sql];

...它消除了内存不足错误。