まず第一に、私はスペイン出身なので、文法について申し訳ありません。私はいくつかのデータをsqliteデータベースに書き込んでいます。これが私のコードです:
@try {
NSFileManager *fileMgr=[NSFileManager defaultManager];
NSString *dbPath=[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:@"capturas.sqlite"];
BOOL succes=[fileMgr fileExistsAtPath:dbPath];
if(!succes)
{
NSLog(@"Cannot locate database '%@'.",dbPath);
}
if (!(sqlite3_open([dbPath UTF8String], &dbcapturas)==SQLITE_OK)) {
NSLog(@"An error has occured: %@",sqlite3_errmsg(dbcapturas));
}
//sqlite3_stmt *sqlStatement;
NSString *asd=numero.text;
NSString *insertStatement=[NSString stringWithFormat:@"INSERT INTO captura(key,tecnico, fecha,provincia,municipio,latitud,longitud,altura,familia,especie,numero,comentario)Values(\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",asd,tecnico,fechaHora,tecnico,municipio,latitud,longitud,altura,tecnico,animal,asd,coment];
char *error;
if((sqlite3_exec(dbcapturas, [insertStatement UTF8String], NULL, NULL, &error))==SQLITE_OK)
{
NSLog(@"Person inserted.");
}
else
{
NSLog(@"Error: %s", error);
}
} @catch (NSException *exception) {
NSLog(@"fail");
}
@finally {
}
保存ボタンを初めてクリックすると、次のようになります。
2012-07-04 12:17:45.644 adasdasd[1783:f803] 人が挿入されました。
そして二度目に私が得る:
2012-07-04 12:29:18.959 adasdasd[1840:f803] エラー: 列キーが一意ではありません
したがって、私のコードは問題ないはずですが、データベースを開くと完全に空になります。