如果存在会返回什么

时间:2012-11-19 16:38:49

标签: ios sql sqlite

我使用sqlite3作为ios项目的数据库,并且想要执行if 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存在的回报是什么?需要一些指导...

1 个答案:

答案 0 :(得分:1)

SQLite没有IF语句。

要检查是否存在某条记录,只需执行类似

的查询
SELECT 1 FROM database WHERE ID = ?

并在您的应用中测试您是否取回记录。

对于更新/插入问题:如果密钥列上有唯一索引,则可以使用INSERT OR REPLACE command,如果新记录会创建重复记录,则会自动删除旧记录。