2012-02-05 89 views
1

我有一個應用程序從圖書館挑選照片,並將其設置到我的應用程序中的UIImageviev。 然後我也將它保存到SQLite表中,但無法通過名稱搜索再次提取它。我知道保存方法是成功的,因爲我手動從終端應用程序控制它。我的提取方法有什麼問題?可以上傳圖像到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; 

你看到了嗎?

+0

抱歉抱歉抱歉!發現問題並解決它。這是因爲我在看第2列而不是第1列...... – Karaca 2012-02-05 21:14:11

回答

0

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

結果集的最左邊的列的索引爲0

你只能選擇2列,編號爲0和1,所以從列#2你的讀操作將失敗。

在sqlite3_bind_blob中,索引從1開始,這就是插入成功的原因。

相關問題