English 中文(简体)
无法在 iPhone 中保存 SQLite 图像
原标题:unable to save image in SQLite in iPhone

can anybody tell me why i am unable to insert an image in sqlite. Some time i get error like: 1)(sqlite3_step(stm) == SQLITE_DONE) , getting value as 21. 2)gets stored in an array and not into databse. I a fresher in objective c so if u could post back a code it would be sweet, as i searched almost evry site. Thanks!

    -(IBAction)submitDetails:(id)sender
    {
               sqlite3_stmt    *stm;
            const char *dbpath = [databasePath UTF8String];
            if (sqlite3_open(dbpath, &connectDB) == SQLITE_OK)
            {

                UIImage *contactImage = imageView.image;
                NSData *imageData = UIImageJPEGRepresentation(contactImage, 100);
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:imageData forKey:@"image"];
                [defaults synchronize];
                NSLog(@"Data saved");             
                NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO STUDENTS (name,salary, gid,photo) VALUES ("%@", "%@", "%@",?)", name.text,salary.text,gid.text,imageData] ;

                const char *insert_stmt = [insertSQL UTF8String];
                sqlite3_prepare_v2(connectDB, insert_stmt, -1, &stm, NULL); 
                NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirec = [paths objectAtIndex:0];
    NSString *imgePath = [documentsDirec stringByAppendingPathComponent:@"note.sqlite"];

    if(sqlite3_open([imgePath UTF8String], &database) == SQLITE_OK){

        const char *sql = "insert into images (images) values (?)";
        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
            UIImage *edtedImae = [info objectForKey:UIImagePickerControllerOriginalImage];
            NSData *dataForImage = UIImagePNGRepresentation(edtedImae);
            sqlite3_bind_blob(addStmt, 1, [dataForImage bytes], [dataForImage length], SQLITE_TRANSIENT);
                NSLog(@"1st ..... %i",sqlite3_step(stm));
                NSLog(@" 2nd.... %i",SQLITE_DONE);
                if (sqlite3_step(stm) == SQLITE_DONE)
                {                   
                    NSString* aName=name.text;
                    NSString* aSalary=salary.text;
                    NSString* aGid=gid.text;

                   UIImage* aPhoto=[UIImage imageWithData:imageData];
                    Person *person = [[Person alloc] initWithName:aName salary:aSalary gid:aGid photo:aPhoto];  
               [rootObject.list addObject:person];
               [person release];                
                    status.text = @"Contact added";
                    NSLog(@"contact added %@  %@",name.text,gid.text);
                    NSLog(@"the count in person list is %i",[rootObject.list count]);
                    name.text = @"";
                    gid.text = @"";
                    salary.text = @"";

                } 
                else
                {
                    status.text = @"Failed to add contact";
                }
                sqlite3_finalize(stm);
                sqlite3_close(connectDB);      
            }


    }
问题回答

如果您可以避免, 不要在数据库中保存图像 。 相反, 请使用列来存储文件名称和任何其他属性, 然后将图像保存为文档 。 我使用一种 guid 方法创建文件名称 。 如果您删除 db 项, 请不要忘记删除文档 。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签