Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting image in to Database using sqlite in xcode4.6

im Working on inserting IMAGE in to Database using sqlite in Xcode 4.6 ,i just wanted to insert a textfield and uiimageview(having dynamically generated barcode image).

unfortunately im getting " Error is: out of memory "

hear is the code I'm using ,plz let me know where my mistake is & Let me know for more details

* 1. Database creation*

-(void)createoropendb {

    NSArray *path =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docpath =[path objectAtIndex:0];

    dbpathstring =[docpath stringByAppendingPathComponent:@"DataBase.db"];
    char *error;

    NSFileManager *filemanager =[NSFileManager defaultManager];

    if (![filemanager fileExistsAtPath:dbpathstring])
    {
        const char *dbpath =[dbpathstring UTF8String];

        if (sqlite3_open(dbpath, &barcodeDB) == SQLITE_OK)
        {
            const char *sql_start ="CREATE TABLE IF NOT EXISTS DBTABLE1(NAME TEXT UNIQUE,IMAGEURL BLOB)";
             NSLog(@"db created");
            sqlite3_exec(barcodeDB, sql_start,NULL,NULL, &error);
            sqlite3_close(barcodeDB);

        }
    }
}

* 2.button to insert data*

- (IBAction)savedata:(id)sender  {
    UIImage *image =imageView.image;
    NSData *imageData=UIImagePNGRepresentation(image);   
    [self SaveImagesToSql: imageData.bytes:Uimagetext.text];  
}

* 3.method to insert data*

- (void) SaveImagesToSql: (NSData*) imgData :(NSString*) mainUrl {   

    const char* sqliteQuery = "INSERT INTO DBTABLE1(NAME,IMAGEURL) VALUES (?, ?)";
    sqlite3_stmt* statement;

    if( sqlite3_prepare_v2(barcodeDB, sqliteQuery, -1, &statement, NULL) == SQLITE_OK )
    {
        sqlite3_bind_text(statement, 1,[mainUrl UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_blob(statement, 2,[imgData bytes],[imgData length], SQLITE_TRANSIENT);

        sqlite3_step(statement);

        NSLog( @"Saved image successfully" );

    }
    else
    {


      NSLog( @"SaveBody: Failed from sqlite3_prepare_v2. Error is:  %s",sqlite3_errmsg(barcodeDB) );
    }
    // Finalize and close database.
    sqlite3_finalize(statement);
}
like image 390
LOVE_2_CODE Avatar asked Nov 13 '22 08:11

LOVE_2_CODE


1 Answers

The return code (which is not SQLITE_OK) is undoubtedly returning SQLITE_MISUSE, meaning that you're trying to use a database that you haven't opened yet. Your createoropendb is creating and opening if the database doesn't exist, but if it does exist, you're not opening it (and even if it did exist, you're closing it after creating it).

Bottom line, if the database is not opened when you call sqlite3_prepare, that function will return SQLITE_MISUSE, any calls to sqlite3_errmsg will return a misleading "Out of memory" text message.

like image 194
Rob Avatar answered Nov 15 '22 07:11

Rob