Hello I have a Sqlite3 database created in my computer and I want to read that database in my Iphone.
The following code works in Iphone simulator, but doesn’t work in a real device, it appears to be some problem related to devices architectures (MacBook vs Iphone).
The problem is when I read the data and try to convert it to UIImage the data doesn’t seem to be correct because the result is a UIImageView in blank instead of the representation of the Image. In the simulator in works nice.
-(IBAction) findPhoto{
databaseName = @"memoryDB.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
int res = 0;
res = sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_READONLY, nil);
if(res == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
NSString *querySQL = [NSString stringWithString:@"SELECT * FROM Fotos"];
const char *sqlStatement = [querySQL UTF8String];
sqlite3_stmt *compiledStatement;
if((res = 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
UIImage *image;
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 1) length:sqlite3_column_bytes(compiledStatement, 1)];
if(data == nil)
NSLog(@"No image found.");
else
image = [[UIImage alloc] initWithData:data];
//
NSLog(@"width: %f,height: %f",image.size.width, image.size.height);
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imgView];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
}
}
I include the database (DB) in the project, and the DB was created in my MacBook. Is that the problem? It is possible to convert the data to the correct format?
This is not proper way. to store image in the database.
you should save it to NSDocumentDirectory.
and just store the name of image into the table of db.
so its easy to get the name of image name from table and get image from documentdirectory. and its very faster then getting image from db.