lundi 29 juin 2015

why the data is not inserting in database sqlite but show data that i take static path and inserted?

////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

Aucun commentaire:

Enregistrer un commentaire