Below is my code to store images in the sqlite database. When I used it to store values it works and now I\'m trying to store images in sqlite database. I don\'t know what I\'m
You should check the return values of bind_text and bind_blob and the step-call, print an error message when they fail.
you just ADD libSqlite3.dylib to Linked FrameWork and Lilbraries and declared database varibles in .h file
//Database Variables
@property (strong, nonatomic) NSString *databasePath;
@property (nonatomic)sqlite3 *contactDB;
@property (strong, nonatomic) IBOutlet UIButton *backbtn;
@property (strong, nonatomic) IBOutlet UIButton *forwardbtn;
drag and drop UIImageView and name to that... i declared as imgView. Goto .m file you just copy and paste that code
int i=1;
long long temp=0;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"images.db"]];
//docsDir NSPathStore2 * @"/Users/gayathiridevi/Library/Application Support/iPhone Simulator/7.0.3/Applications/B5D4D2AF-C613-45F1-B414-829F38344C2A/Documents" 0x0895e160
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS IMAGETB (ID INTEGER PRIMARY KEY AUTOINCREMENT,URL TEXT, CHECKSUM TEXT,IMAGE BLOB)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog( @"User table Not Created Error: %s", errMsg);
}
else
{
NSLog( @"User table Created: ");
}
sqlite3_close(_contactDB);
}
else {
NSLog( @"DB Not Created");
}
}
[self saveImage];
[self showImage];
}
- (void)saveImage
{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
NSString *insertSQL=@"INSERT INTO IMAGETB(URL,image) VALUES(?,?)";
if(sqlite3_prepare_v2(_contactDB, [insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL)== SQLITE_OK)
{
//NSString *url =@"https://lh6.googleusercontent.com/-vJBBGUtpXxk/AAAAAAAAAAI/AAAAAAAAADQ/nfgVPX1n-Q8/photo.jpg";
//NSString *url =@"http://upload.wikimedia.org/wikipedia/commons/2/2a/Junonia_lemonias_DSF_upper_by_Kadavoor.JPG";
// NSString *url =@"http://upload.wikimedia.org/wikipedia/commons/8/84/Tibia_insulaechorab.jpg";
NSString *url =@"http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/PNG_transparency_demonstration_2.png/280px-PNG_transparency_demonstration_2.png";
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
NSData *imageData=UIImagePNGRepresentation(image);
sqlite3_bind_text(statement,1, [url UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob(statement, 2, [imageData bytes], [imageData length], SQLITE_TRANSIENT);
NSLog(@"Length from internet : %lu", (unsigned long)[imageData length]);
}
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog( @"Insert into row id %lld",(sqlite3_last_insert_rowid(_contactDB)));
temp =(sqlite3_last_insert_rowid(_contactDB));
}
else {
NSLog( @"Error IN INSERT" );
}
sqlite3_finalize(statement);
sqlite3_close(_contactDB);
}
}
- (void)showImage
{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if(sqlite3_open(dbpath,&_contactDB)==SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"Select IMAGE FROM IMAGETB WHERE ID = %d",i];
if(sqlite3_prepare_v2(_contactDB,[insertSQL cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {
while(sqlite3_step(statement) == SQLITE_ROW) {
int length = sqlite3_column_bytes(statement, 0);
NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length];
NSLog(@"Length from db : %lu", (unsigned long)[imageData length]);
if(imageData == nil)
NSLog(@"No image found.");
else
_imgView.image = [UIImage imageWithData:imageData];
NSLog(@"image found.");
}
}
sqlite3_finalize(statement);
}
sqlite3_close(_contactDB);
}
-(IBAction)backBtn:(id)sender {
if (i<=1) {}
else{
i=i-1;
[self showImage];
}
}
-(IBAction)forwardBtn:(id)sender {
if(i==temp){}
else{
i=i+1;
[self showImage];
}
}
I answered a similair question with this answer: It's better if you use CoreData instead. It will be much easier for you to work with CoreDate instead of SQL. CoreData is pretty much an SQL database in a nice wrapper.
If you use iOS 5 you could easily add images to the database without having to worry about them being BLOBS (Binary Large Object) by checking "Allows External Storage".
const char *insertSQL="insert into Persons(PersonName,CompanyName,ImgUrl,PersonImage)values(?,?)"
You have 4 values to insert into your table & only 2 placeholders for the parameters. Correct them.
Heck I ain't an iOS developer