使用以下两个函数尝试写入SQL数据库时出错:
- (void)viewDidLoad
NSString *dbName = @"TrackDate.sqlite";
[dbName retain];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
NSString *dbPath = [docDir stringByAppendingPathComponent:dbName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL exists = [fileManager fileExistsAtPath:dbName];
if (exists == false) {
NSString *resourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbName];
[fileManager copyItemAtPath:resourcePath toPath:dbPath error:nil];
[fileManager release];
}
sqlite3_open ([dbPath UTF8String], &database);
}
正在使用变量
char *sql = "Insert into Daten (Latitude, Longitude, Altitude, Speed, Course, Time) Values (?, ?, ?, ?, ?, ?, ?)";
sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL);
sqlite3_bind_text(addStmt, 1, [latitude UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [longitude UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [altitude UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 4, [speed UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 5, [course UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 6, [date UTF8String], -1, SQLITE_TRANSIENT);
if (SQLITE_DONE != sqlite3_step(addStmt)) {
recordLabel.text = @"Error!";
}
sqlite3_reset(addStmt);
}
答案 0 :(得分:1)
char *sql = "Insert into Daten (Latitude, Longitude, Altitude, Speed, Course, Time) Values (?, ?, ?, ?, ?, ?, ?)";
SQL语句中有一个错误:六列,七个占位符。
答案 1 :(得分:0)
您传递了六个字段名称并分配了七个值。
替换此
char * sql =“插入日期(纬度,经度,海拔高度,速度,航线,时间)值(?,?,?,?,?,?,?)”;
带
char * sql =“插入日期(纬度,经度,海拔高度,速度,航线,时间)值(?,?,?,?,?,?)”;
答案 2 :(得分:-1)
sqlite3_stmt *statement = nil;
char sql[1000];
write values according to your self.
sprintf(sql, "Insert into *your table name* values ('%s', '%s')",[productId UTF8String], [folderId UTF8String]);
// path将包含数据库位置的路径。
NSString *path = [self GetDatabasePath] ;
if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
{
if((sqlite3_prepare_v2(database, sql, -1, &statement, NULL)) == SQLITE_OK)
{
//NSLog(@"%s",sql);
sqlite3_step(statement);
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
快乐的编码!!