簡體   English   中英

無法在iOS中將圖像更新為SQLite

[英]Fail to update the image to SQLite in ios

我正在按照以下方式創建表,並將數據插入到該表中,並且檢索了表值以及成功創建,插入和檢索的圖像,但未更新下面的sqlite表中的圖像是我的整個代碼幫助我先謝謝了
這是創建代碼

-(int) createTable:(NSString*) filePath
{
    sqlite3* db = NULL;
    int rc=0;
    NSLog(@"create");
    rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (SQLITE_OK != rc)
    {
        sqlite3_close(db);
        NSLog(@"Failed to open db connection");
    }
    else
    {
        char * query ="CREATE TABLE IF NOT EXISTS places ( id INTEGER PRIMARY KEY AUTOINCREMENT,place TEXT ,locationname TEXT,time TEXT,description TEXT,notifyTime TEXT,radius TEXT,lat DOUBLE,lon DOUBLE ,notify TEXT,selection INTEGER,status INTEGER,radiusMeter DOUBLE,frequency INTEGER,notifiedStatus INTEGER,snoozeNooftimes INTEGER,snoozeStatus INTEGER,snoozeTime TEXT,reminderImage BLOB)";
        char * errMsg;
        rc = sqlite3_exec(db, query,NULL,NULL,&errMsg);

        if(SQLITE_OK != rc)
        {
            NSLog(@"Failed to create table rc:%d, msg=%s",rc,errMsg);
        }
        else{
            NSLog(@"Sucessfully Created ");
        }

        sqlite3_close(db);
    }
    return rc;
}

上面的字段成功創建了表格。下面是我的插入代碼。

-(int) insert:(NSString *)filePath withName:(NSString *)place location:(NSString *)locationString description:(NSString*)description time:(NSString*)time notify:(NSString *)notifyTime radius:(NSString *)radius lat:(double)lat lon:(double)lon notify:(NSString *)notify selec:(int)selection stat:(int)status radius:(double)radiusMeter freq:(int)frequency notifyStatus:(int)notifiedStatus snoozeNooftime:(int)snoozeNoOfTimes snoozeStatus:(int)snoozeStat snoozeTime:(NSString *)snoozeTime img:(NSData *)imageData
{
    sqlite3* db = NULL;
    int rc=0;
   sqlite3_stmt *stmt =NULL;
    rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE , NULL);
    if (SQLITE_OK != rc)
    {
        sqlite3_close(db);
        NSLog(@"insert Failed to open db connection");
    }
    else
    {
          const char *insertSQL="INSERT INTO places (place,locationname,time,description,notifyTime,radius,lat,lon,notify,selection,status,radiusMeter,frequency,notifiedStatus,snoozeNooftimes,snoozeStatus,snoozeTime,reminderImage) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        if (sqlite3_prepare_v2(db, insertSQL, -1, & stmt, NULL) != SQLITE_OK)
        {
            NSLog(@"INSERT fails error: %s", sqlite3_errmsg(db));
        }
        else
        {
            sqlite3_bind_text(stmt, 1, [place UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 2, [locationString UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 3, [time UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 4, [description UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 5, [notifyTime UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 6, [radius UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_double(stmt, 7, lat);
            sqlite3_bind_double(stmt, 8, lon);
            sqlite3_bind_text(stmt, 9, [notify UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_int(stmt, 10, selection );
            sqlite3_bind_int(stmt, 11, status);
            sqlite3_bind_double(stmt, 12, radiusMeter);
            sqlite3_bind_int(stmt, 13, frequency);
            sqlite3_bind_int(stmt, 14, notifiedStatus);
            sqlite3_bind_int(stmt, 15, snoozeNoOfTimes);
            sqlite3_bind_int(stmt, 16, snoozeStat);
            sqlite3_bind_text(stmt, 17, [snoozeTime UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_blob(stmt, 18, [imageData bytes], [imageData length], SQLITE_TRANSIENT);

            int success = sqlite3_step(stmt);
            sqlite3_reset(stmt);
            if (success == SQLITE_ERROR)
            {

            }
            else
            {
//                 NSLog(@"insert query %@",query);
            }
            sqlite3_finalize(stmt);
            sqlite3_close(db);
        }
    }
    return rc;
}

我的更新代碼如下:

-(void) update:(NSString *)filePath withName:(NSString *)place location:(NSString *)locationString description:(NSString*)description time:(NSString*)time notify:(NSString *)notifyTime radius:(NSString *)radius lat:(double)lat lon:(double)lon notify:(NSString *)notify selec:(int)selection where:(int)idd stat:(int)status radius:(double)radiusMeter freq:(int)frequency notifyStatus:(int)notifiedStatus snoozeNooftime:(int)snoozeNoOfTimes snoozeStatus:(int)snoozeStat snoozeTime:(NSString *)snoozeTime img:(NSData *)imageData
{
    sqlite3* db = NULL;
    int rc=0;
    sqlite3_stmt* stmt =NULL;

    rc = sqlite3_open_v2([filePath cStringUsingEncoding:NSUTF8StringEncoding], &db, SQLITE_OPEN_READWRITE , NULL);
    if (SQLITE_OK != rc)
    {
        sqlite3_close(db);
        NSLog(@"update all Failed to open db connection");
    }
    else
    {
        const char *sql = "UPDATE  places SET place = ?, locationname = ?, time = ?, description = ?, notifyTime = ?, radius = ?, lat = ?, lon = ?, notify = ?, selection = ?, status = ?, radiusMeter = ?, frequency = ?, notifiedStatus = ?,snoozeNooftimes = ?,snoozeStatus = ?,snoozeTime = ? reminderImage = ? where id = ?";

        if (sqlite3_prepare_v2(db, sql, -1, & stmt, NULL) != SQLITE_OK)
        {
             NSLog(@"update all fails error: %s", sqlite3_errmsg(db));
        }
        else
        {           
            sqlite3_bind_text(stmt, 1, [place UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 2, [locationString UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 3, [time UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 4, [description UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 5, [notifyTime UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_text(stmt, 6, [radius UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_double(stmt, 7, lat);
            sqlite3_bind_double(stmt, 8, lon);
            sqlite3_bind_text(stmt, 9, [notify UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_int(stmt, 10, selection );
            sqlite3_bind_int(stmt, 11, status);
            sqlite3_bind_double(stmt, 12, radiusMeter);
            sqlite3_bind_int(stmt, 13, frequency);
            sqlite3_bind_int(stmt, 14, notifiedStatus);
            sqlite3_bind_int(stmt, 15, snoozeNoOfTimes);
            sqlite3_bind_int(stmt, 16, snoozeStat);
            sqlite3_bind_text(stmt, 17, [snoozeTime UTF8String], -1, SQLITE_TRANSIENT);
            sqlite3_bind_blob(stmt, 18, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
             sqlite3_bind_int(stmt, 0, idd);

            int success = sqlite3_step(stmt);
            sqlite3_reset(stmt);
            if (success == SQLITE_ERROR)
            {
            }
            else
            {
                NSLog(@"update all success");
            }
            sqlite3_finalize(stmt);
            sqlite3_close(db);
        }
    }
}

當我更新它得到以下錯誤。 更新全部失敗錯誤:“ reminderImage”附近:語法錯誤

UPDATE places SET place = ?, ... ,snoozeTime = ? reminderImage = ? where...

之間的逗號snoozeTimereminderImage丟失。

暫無
暫無

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

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