您好,我正在开发一个使用 sqlitedb 的应用程序。我阅读了一些示例并尝试创建我的应用程序。我要插入的表有 3 列。ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, DURATION REAL, TIME REAL 我根据我读过的例子写了下面的代码。但我总是看到添加记录失败。PS:我测试了数据库创建,没问题。我用这个教程 在另一个应用程序上没问题。但这里不起作用
NSTimeInterval endTime = [[NSDate date] timeIntervalSinceReferenceDate];
double elapsedTime = endTime-startcache;
NSString *my=[NSString stringWithFormat:@"%f",elapsedTime];
TextField1.text=my;
// saving in database
NSString * durationstring=[NSString stringWithFormat:@"%f",elapsedTime];
NSString * timestring=[NSString stringWithFormat:@"%f",endTime];
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_activityDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:
@"INSERT INTO ACTIVITYTABLE (name, duration,time ) VALUES (\"%@\", \"%@\", \"%@\")",
activityname, durationstring,timestring];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_activityDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
self.status.text = @"record added";
} else {
self.status.text = @"Failed to add record";
}
sqlite3_finalize(statement);
sqlite3_close(_activityDB);
}