Sqlite資料庫插入和讀取圖片資料

來源:互聯網
上載者:User

  在iOS下用sqlite資料庫儲存圖片,先把你的圖片轉換成 NSData 形式,然後在資料庫添加一行 blob 資料

  假定資料庫中存在表 test_table(name,image), 下面代碼將圖片檔案test.png的位元據寫到sqlite資料庫:

  CHAR *name = "test";

  NSString * nameString = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];

  NSString * filePath = [[NSBundle mainBundle] pathForResource:nameString ofType:@"png"];

  IF ([[NSFileManager defaultManager] fileExistsAtPath:filePath])

  {

  NSData * imgData = UIImagePNGRepresentation([UIImage imageWithContentsOfFile:filePath]);

  const CHAR * sequel = "insert into test_table(name,image) values(?,?)";

  sqlite3_stmt * UPDATE;

  IF (sqlite3_prepare_v2(DATABASE, sequel, -1, &UPDATE, NULL) == SQLITE_OK)

  {

  sqlite3_bind_text(UPDATE, 1, name, -1, NULL);

  sqlite3_bind_blob(UPDATE, 2, [imgData bytes], [imgData LENGTH], NULL);

  IF( sqlite3_step(UPDATE) == SQLITE_DONE)

  {

  NSLog(@"已經寫入資料");

  }

  sqlite3_finalize(UPDATE);

  }

  }

  ELSE

  {

  NSLog(@"檔案不存在");

  }

  下面代碼從資料庫中讀取圖片位元據,然後顯示圖片:

  const CHAR * sequel = "select image from test_table where name=?";

  sqlite3_stmt * getimg;

  IF (sqlite3_prepare_v2(DATABASE, sequel, -1, &getimg, NULL) == SQLITE_OK)

  {

  CHAR *name = "test";

  sqlite3_bind_text(UPDATE, 1, name, -1, NULL);

  IF(sqlite3_step(getimg) == SQLITE_ROW)

  {

  INT bytes = sqlite3_column_bytes(getimg, 0);

  Byte * VALUE = (Byte*)sqlite3_column_blob(getimg, 1);

  IF (bytes !=0 && VALUE != NULL)

  {

  NSData * DATA = [NSData dataWithBytes:VALUE LENGTH:bytes];

  UIImage * img = [UIImage imageWithData:DATA];

  UIImageView * aview =[[UIImageView alloc] initWithFrame:

  CGRectMake(0.0, 0.0, IMAGE_WIDTH, IMAGE_HEIGHT)];

  aview.image = img;

  [SELF.VIEW addSubview:aview];

  [aview release];

  }

  }

  sqlite3_finalize(getimg);

  }

  例2:儲存圖片

  / Save Small Image Data by given main url

  - (void) SaveImagesToSql: (NSData*) imgData :(NSString*) mainUrl

  {

  NSLog( @"n*****Save image to SQLite*****n" );

  const char* sqliteQuery = "INSERT INTO IMAGES (URL, IMAGE) VALUES (?, ?)";

  sqlite3_stmt* statement;

  if( sqlite3_prepare_v2(articlesDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )

  {

  sqlite3_bind_text(statement, 1, [mainUrl UTF8String], -1, SQLITE_TRANSIENT);

  sqlite3_bind_blob(statement, 2, [imgData bytes], [imgData length], SQLITE_TRANSIENT);

  sqlite3_step(statement);

  }

  else NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(articlesDB) );

  // Finalize and close database.

  sqlite3_finalize(statement);

  }

  讀取圖片:

  // Load images from data base with given image url

  - (NSData*) LoadImagesFromSql: (NSString*) imageLink

  {

  NSData* data = nil;

  NSString* sqliteQuery = [NSString stringWithFormat:@"SELECT IMAGE FROM IMAGES WHERE URL = '%@'", imageLink];

  sqlite3_stmt* statement;

  if( sqlite3_prepare_v2(articlesDB, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK )

  {

  if( sqlite3_step(statement) == SQLITE_ROW )

  {

  int length = sqlite3_column_bytes(statement, 0);

  data = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];

  }

  }

  // Finalize and close database.

  sqlite3_finalize(statement);

  return data;

  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.