iphone sqlite 插入... EXC_BAD_ACCESS
首先,我对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要释放时间戳,您不会分配或保留它。
Don't release timestamp, you're not allocating or retaining it.