1
我正在使用sqlite3作爲ios項目的數據庫,並且想要執行exists語句以查看對象是否存在,如果發生更新或插入。如果存在什麼返回
我使用這個功能來查詢數據庫
-(void)updateStatus:(NSString *)queryString {
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
m_singleton = [Singleton sharedSingleton];
strDatabasePath = [NSString stringWithString:[docsDir stringByAppendingPathComponent:@"BorneoMotors.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: strDatabasePath] == YES)
{
const char *dbpath = [strDatabasePath UTF8String];
if (sqlite3_open(dbpath, &sqlDatabase) == SQLITE_OK)
{
const char* beginString = "BEGIN;";
sqlite3_stmt *compiledstatement;
sqlite3_prepare_v2(sqlDatabase, beginString, -1, &compiledstatement, NULL);
if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
else NSLog(@"Failed!");
sqlite3_finalize(compiledstatement);
NSLog(@"QUERY : %@",queryString);
const char *selectStatement = [queryString UTF8String];
sqlite3_prepare_v2(sqlDatabase, selectStatement, -1, &compiledstatement, NULL);
//sqlite3_bind_text(compiledstatement,1,[statusString UTF8String],-1,SQLITE_TRANSIENT);
if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
else NSLog(@"Failed!");
sqlite3_finalize(compiledstatement);
const char* endString="END;";
sqlite3_prepare_v2(sqlDatabase, endString, -1, &compiledstatement, NULL);
if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
else NSLog(@"Failed!");
sqlite3_finalize(compiledstatement);
sqlite3_close(sqlDatabase);
}
else NSLog(@"Failed to open table");
}
}
我的問題是,這是什麼,如果存在,將返回和如何移動在這。
NSString *checkstring = [NSString stringWithFormat:@"IF EXISTS (SELECT * FROM DATABASE WHERE id = %@",cars.ID];
DB *accessdb = [[DB alloc] init];
[accessdb updateStatus:checkstring];
if (??){
NSString *queryString = [NSString stringWithFormat: @"UPDATE DATABASE SET STATUS='%@' WHERE \"DATABASEID\" = '%@'", cars.status,cars.ID];
const char *selectStatement = [queryString UTF8String];
sqlite3_prepare_v2(sqlDatabase, selectStatement, -1, &statement, NULL);
//sqlite3_bind_text(compiledstatement,1,[statusString UTF8String],-1,SQLITE_TRANSIENT);
if (sqlite3_step(statement) == SQLITE_DONE) {}
else NSLog(@"Failed!");
sqlite3_finalize(statement);
}else{
insert}
如何檢查對象是否存在? if存在什麼回報?需要一些指導...