簡體   English   中英

如何逐行更新我的sqlite數據庫? iOS

[英]How can I update my sqlite database one row by row ? IOS

我想像Android一樣逐行更新數據庫sqlite3,並帶有用於更新特定行的索引。 我該如何使用呢?

-(void) updateBddWithData:(Data*) data{ 

    sqlite3 *databaseOK=self.database;

    if(sqlite3_open([self.databasePath UTF8String], &databaseOK) == SQLITE_OK) {

        NSString* updateIntoBdd = [NSString stringWithFormat:@"UPDATE ART_TABLE SET ida = '%@' ,title = '%@' ,chapo = '%@' ,link = '%@' ,linkimg = '%@' ,pubdate = '%@' ,creator = '%@' ,description = '%@'  ",data.ide,data.title,data.chapo,data.link,data.linkImg,data.pubDate,data.creator,data.description];

        sqlite3_stmt* statement = NULL;

        int returnValue = (sqlite3_prepare_v2(databaseOK, [updateIntoBdd UTF8String], -1, &statement, NULL));

        if(returnValue == SQLITE_OK){

            if(sqlite3_step(statement) == SQLITE_DONE){

             // What can I do ?

             }

        }
        sqlite3_finalize(statement);
        sqlite3_close(databaseOK);
    }
}

當我調用方法Update(object)時,我只想用我的參數對象更新一個索引行。

謝謝前進

為了避免重新發明輪子,我建議使用FMDatabase這樣的包裝器。

但是,如果您確實想在沒有包裝的情況下進行操作:

-(void) updateBddWithData:(Data*) data { 

    sqlite3 *databaseOK=self.database;

    if(sqlite3_open([self.databasePath UTF8String], &databaseOK) == SQLITE_OK) {

        NSString* updateIntoBdd = [NSString stringWithFormat:@"INSERT OR REPLACE INTO ART_TABLE (ida, title, chapo, link, linkimg, pubdate, creator, description) VALUES ('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", data.ide, data.title, data.chapo, data.link, data.linkImg, data.pubDate, data.creator, data.description];

        sqlite3_stmt* statement = NULL;

        const char *update_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(databaseOK, insert_stmt,-1, &update_stmt, NULL);

        if (sqlite3_step(statement) == SQLITE_DONE) {
             //SUCCESS
        } 
        else {
            //ERROR
        }

        sqlite3_reset(statement);

        sqlite3_close(databaseOK);
    }
}

您在語句中缺少WHERE子句。

通常,您在表中有一個ID (也許是您的ida嗎?),因此您可以使用:

UPDATE ART_TABLE SET ida = '%@', ... WHERE id ='%@';

還可以考慮使用更安全的sqlite3_bind_ *

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM