NSImage for blob and blob for NSImage (SQLite, NSData)

First of all, I know that I should not use the SQLite database to store the image, but I only store website icons that are really small.

My problem is that I am trying to insert these icons into the database (it seems to work), I convert favicon to NSData using the -tiffrepresentation NSimage method, and then paste it into my database in the blob column:

  NSImage *favico = [webview mainFrameIcon]; [appDelegate insertBookmark:[titleField stringValue] url:[urlfield stringValue] data:[favico TIFFRepresentation]] 

The SQLite method is as follows:

 -(void)insertBookmark:(NSString *)title url:(NSString *)url data:(NSData *)data { NSData *imagedata = [[NSData alloc]initWithData:data]; sqlite3 *database; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { NSString *query = [NSString stringWithFormat:@"INSERT INTO Bookmarks (title, url, image) VALUES ('%@', '%@', '%@')",title, url, data]; const char *querychar = [query UTF8String]; sqlite3_stmt *statement; if (sqlite3_prepare_v2(database, querychar, -1, &statement, NULL) == SQLITE_OK) { int row = 3; sqlite3_bind_blob(statement, row, [imagedata bytes], [imagedata length], NULL); sqlite3_step(statement); sqlite3_finalize(statement); } else { NSLog(@"Error"); } [databasePath retain]; [databaseName retain]; } sqlite3_close(database); [imagedata release]; } 

When I browse the database, I have a value in the image column between <data> (normal?)

Now, when I retrieve the blob from the database and try to put it in my object, I got null in NSimage:

 -(void) readDatabase { // Setup the database object sqlite3 *database; bookmarks = [[NSMutableArray alloc] init]; // Open the database from the users filessytem if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // Setup the SQL Statement and compile it for faster access const char *sqlStatement = "SELECT * FROM Bookmarks ORDER BY title ASC"; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { // Loop through the results and add them to the feeds array while(sqlite3_step(compiledStatement) == SQLITE_ROW) { // Read the data from the result row NSString *aTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; NSString *aUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 3) length:sqlite3_column_bytes(compiledStatement, 3)]; NSImage *image = [[NSImage alloc]initWithData:data]; bookmarkObject *bookmark = [[bookmarkObject alloc] initWithName:aTitle url:aUrl favico:image]; [bookmarks addObject:bookmark]; [bookmark release]; [data release]; [image release]; } } // Release the compiled statement from memory sqlite3_finalize(compiledStatement); sqlite3_close(database); [databasePath retain]; [databaseName retain]; } 

}

Thanks in advance

+7
source share
1 answer

< and > around your actual data comes from NSData. Using %@ in your format string and providing data , NSString sends a description to data . [NSData description] wraps content between < and > .

Another problem with your code is that you seem to be mixing instructions with parameters and parameters:

  • Use stringWithFormat: to create a full query
  • or use a query like INSERT INTO Bookmarks (title ...) VALUES (? ...) in combination with sqlite3_bind_blob

The syntax syntax for SQLite support parameters can be found here: http://www.sqlite.org/c3ref/bind_blob.html

+4
source

All Articles