0

////this method use for show data and that works

    NSString *databasePath =[[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"images.db"];

    if(sqlite3_open([databasePath UTF8String], &db)==SQLITE_OK){

        const char *sql = "SELECT * FROM IMAGEDATA";
        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
           NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
        }
    else{

          while (sqlite3_step(sqlStatement)==SQLITE_ROW) {

               NSData *imageData = [[NSData alloc] initWithBytes:sqlite3_column_blob(sqlStatement, 1) length: sqlite3_column_bytes(sqlStatement, 1)];

               imagesfromdb =[UIImage imageWithData:imageData];
               NSLog(@"IMAGE :::%@",imagesfromdb);
              [databaseimage addObject:imagesfromdb];

             //imageData is saved favicon from website.

       }
    }
  }

/////this method use for insert data.

   const char *dbpath = [databasePath UTF8String];

  if(sqlite3_open(dbpath, &database)==SQLITE_OK)
  {

    NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1);   
    const char* sqliteQuery = "INSERT INTO IMAGEDATA (NAME, IMAGE) VALUES (?, ?)";
   //sqlite3_stmt* statement;

   if( sqlite3_prepare_v2(database, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
   {
       sqlite3_bind_text(statement, 1, [fileName UTF8String], -1, SQLITE_TRANSIENT);
       sqlite3_bind_blob(statement, 2, [imageData bytes], (int)[imageData length], SQLITE_TRANSIENT);
       sqlite3_step(statement);
   }
  else NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s", sqlite3_errmsg(database) );

   // Finalize and close database.
   sqlite3_finalize(statement);

   }

this method not working i cant understand but this method show data until i close the app. but after that the data will be gone and didnt save in db file

soumya
  • 3,801
  • 9
  • 35
  • 69
vp2698
  • 1,783
  • 26
  • 28

1 Answers1

0

All the assets inside mainBundle of your app sandbox have readonly property. That's why in your case you are able to read the data but not modify it. So you should first copy your database file to DocumentDirectory which has readwrite access, and then you will be able to perform the write operation in that db. Check this question to understand how to copy database file from mainBundle to DocumentDirectory.

How to copy sqlite database when application is launched in iOS?

You can follow this Tutorial as well to understand all steps

Community
  • 1
  • 1
Gandalf
  • 2,399
  • 2
  • 15
  • 19