iphone sqlite插入。。。EXC_坏访问
首先,我对iPhone的开发相当陌生,对在我的应用程序中加入sqlite3也是一个全新的概念。我在Android和Blackberry应用程序中使用过它,所以我知道基本命令等等。这是我的问题:我以编程方式创建了一个db和一个表(两者都是正确创建的)。下面是我用来创建表格的代码:iphone sqlite插入。。。EXC_坏访问,iphone,sqlite,insert,exc-bad-access,Iphone,Sqlite,Insert,Exc Bad Access,首先,我对iPhone的开发相当陌生,对在我的应用程序中加入sqlite3也是一个全新的概念。我在Android和Blackberry应用程序中使用过它,所以我知道基本命令等等。这是我的问题:我以编程方式创建了一个db和一个表(两者都是正确创建的)。下面是我用来创建表格的代码: const char *sql_stmt = "CREATE TABLE IF NOT EXISTS EXPENSES (id integer primary key autoincrement, unix_date i
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_访问权限。我认为这个错误通常在NSString值被指定为string值时出现
有什么想法吗?不要释放时间戳,你没有分配或保留它
[timestamp release];
不要释放时间戳,您没有分配或保留它
[timestamp release];
啊!!就这样。仍在掌握内存管理的过程中…一直在使用Java。谢谢啊!!就这样。仍在掌握内存管理的过程中…一直在使用Java。谢谢