可以将图像上传到SQLite但可以再次提取它

时间:2012-02-05 21:11:00

标签: database image sqlite png extract

我有一个应用程序,从库中选择照片并将其设置为我的应用程序中的UIImageviev。 然后我也将它保存到SQLite表中,但不能通过名称搜索再次提取它。我知道保存方法是成功的,因为我手动控制终端应用程序。我的提取方法有什么问题???

//Save the photo which is currently in an UIImage Variable into the SQLITE photos as BLOB
-(IBAction)SaveImage{
        sqlite3_stmt *save_statement;
        const char *dbpath = [databasePath UTF8String];
        NSString *insertSQL = [NSString stringWithFormat: @"insert into photos (name, photo) values(?, ?)"];
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_open(dbpath, &photosDB);
        sqlite3_prepare(photosDB, insert_stmt, -1, &save_statement, NULL);
        sqlite3_bind_text(save_statement, 1, [text.text UTF8String], -1, SQLITE_TRANSIENT);
        NSData *binData = UIImagePNGRepresentation(image.image);
        if (sqlite3_bind_blob(save_statement, 2, [binData bytes], [binData length], SQLITE_TRANSIENT)==SQLITE_OK) {
        status.text = @"Gonderi Kaydedildi";}else{status.text = @"Tanimlanamayan bir sorun var";}
        sqlite3_step(save_statement);
        sqlite3_finalize(save_statement);
        sqlite3_close(photosDB);}

下面我有一些问题...

//Get the photo from database construct it and set it into the UIImageView variable in our application
-(IBAction)ImageFromDatabase{

        sqlite3_stmt *call_statement;
        const char *dbpath = [databasePath UTF8String];
        NSString *findSQL = [NSString stringWithFormat: @"SELECT name, photo FROM photos WHERE name=\"%@\"", [text text]];
        const char *sql = [findSQL UTF8String];
        sqlite3_open(dbpath, &photosDB);
        sqlite3_prepare(photosDB, sql, -1, &call_statement, NULL);
        if (sqlite3_step(call_statement) == SQLITE_ROW) {
            NSString *imageName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(call_statement, 0)];
            status.text = imageName;

            const char *rawData = sqlite3_column_blob(call_statement, 2);
            int rawDataLength = sqlite3_column_bytes(call_statement, 2);
            NSData *data = [NSData dataWithBytes:rawData length:rawDataLength];
            UIImage *image1 = [[UIImage alloc]initWithData:data];
            image.image = image1;}

        sqlite3_step(call_statement);
        sqlite3_finalize(call_statement);
        sqlite3_close(photosDB);
}

我觉得问题低于esspecilly但是?!?!?!?!

    const char *rawData = sqlite3_column_blob(call_statement, 2);
    int rawDataLength = sqlite3_column_bytes(call_statement, 2);
    NSData *data = [NSData dataWithBytes:rawData length:rawDataLength];
    UIImage *image1 = [[UIImage alloc]initWithData:data];
    image.image = image1;

你看到了吗???

1 个答案:

答案 0 :(得分:0)

您要从第2列中提取照片。引用SQLite reference page;

  

结果集的最左列具有索引0

您只选择了2列,编号为0和1,因此您从第2列的读取将失败。

在sqlite3_bind_blob中,索引从1开始,这就是你的插入成功的原因。