Objective C 中的 SQLite 内存不足错误

发布于 2024-12-11 08:08:24 字数 1571 浏览 0 评论 0原文

我找到了一个关于带有 Objective C 的 sqlite 的示例并使用了我的项目。但我有一个问题。创建添加语句时。该错误是“内存不足”异常。我该如何修复这个错误?

在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添加方法:

- (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);
}

I found an example about sqlite with objective c and used my project. But I have a problem. when creating add statement. The error is 'out of memory' exeption. How can I fixed this error?

in viewcontroller class:

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 method:

- (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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-12-18 08:08:24

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

success = [dbHandlerexecuteUpdate:sql];

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

In my case, I had copied and pasted some code and I was trying to execute an SQL statement after I had closed the DB connection. I removed the erroneous line:

success = [dbHandler executeUpdate:sql];

...and it removed the out of memory error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文