iphone sqlite 插入... EXC_BAD_ACCESS

发布于 2024-12-08 01:23:05 字数 1503 浏览 2 评论 0原文

首先,我对 iPhone 开发相当陌生,并且对于将 sqlite3 合并到我的应用程序中也是全新的。我已经在我的 Android 和 Blackberry 应用程序中使用过它,所以我知道基本命令等。这是我的问题:我以编程方式创建了一个数据库和表(两者均已正确创建)。这是我用来创建表格的代码:

const char *sql_stmt = "CREATE TABLE IF NOT EXISTS EXPENSES (id integer primary key autoincrement, unix_date integer, date_time text, purpose text, start_mile text, end_mile text, distance text, fees text, party_id integer)";

表格已正确创建。

这是我用来保存数据的函数:

- (void) saveData
{
sqlite3_stmt    *statement;
const char *dbpath = [databasePath UTF8String];
NSString *timestamp = [NSString stringWithFormat:@"%d", (long)[[NSDate date] timeIntervalSince1970]];

if (sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)
{
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO EXPENSES (timestamp, date_time, purpose, start_mile, end_mile, distance, fees, party_id) VALUES (\"%d\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%d\")", 9, @"date", @"purpose", @"start", @"end", @"miles", @"tolls", 0];
    const char *insert_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(expenseDB, insert_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
     //   status.text = @"Contact added";

    } else {
      //  status.text = @"Failed to add contact";
    }
    sqlite3_finalize(statement);
    sqlite3_close(expenseDB);
}
[timestamp release];

}

当我运行此代码时,我从主线程获得 EXC_BAD_ACCESS。我认为当 NSString 值被分配为字符串值时通常会出现此错误?

有什么想法吗?

First of all, I'm fairly new to iPhone development, and brand new to incorporating sqlite3 in my app. I have used it in my Android and Blackberry apps, so I know basic commands and such. Here is my problem: I created a db and table programmatically (both were created correctly). Here is the code I used to creative the table:

const char *sql_stmt = "CREATE TABLE IF NOT EXISTS EXPENSES (id integer primary key autoincrement, unix_date integer, date_time text, purpose text, start_mile text, end_mile text, distance text, fees text, party_id integer)";

The table was created properly.

This the function I used to save data:

- (void) saveData
{
sqlite3_stmt    *statement;
const char *dbpath = [databasePath UTF8String];
NSString *timestamp = [NSString stringWithFormat:@"%d", (long)[[NSDate date] timeIntervalSince1970]];

if (sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)
{
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO EXPENSES (timestamp, date_time, purpose, start_mile, end_mile, distance, fees, party_id) VALUES (\"%d\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%d\")", 9, @"date", @"purpose", @"start", @"end", @"miles", @"tolls", 0];
    const char *insert_stmt = [insertSQL UTF8String];
    sqlite3_prepare_v2(expenseDB, insert_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
     //   status.text = @"Contact added";

    } else {
      //  status.text = @"Failed to add contact";
    }
    sqlite3_finalize(statement);
    sqlite3_close(expenseDB);
}
[timestamp release];

}

When I run this code, I get a EXC_BAD_ACCESS from the main thread. I thought this error generally showed up when an NSString value was assigned as string value?

Any thoughts?

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

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

发布评论

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

评论(1

杀お生予夺 2024-12-15 01:23:05

不要释放时间戳,您不会分配或保留它。

[timestamp release];

Don't release timestamp, you're not allocating or retaining it.

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