Iphone 未存储到数据库的图像

Iphone 未存储到数据库的图像,iphone,json,uiimageview,nsdata,Iphone,Json,Uiimageview,Nsdata,我在服务器中存储了一些图像。并尝试从URL获取这些图像并将其存储到本地数据库,然后尝试访问。但这些图像并没有存储到数据库中。但是当我使用log时,它显示插入所有值。但是我不能通过SQLItemManager在db中显示 代码: 按钮点击: -(void)click:(id)sender{ NSLog(@"click"); NSString *databasePath=[[NSBundle mainBundle]pathForResource:@"db1" ofType:@"sqli

我在服务器中存储了一些图像。并尝试从URL获取这些图像并将其存储到本地数据库,然后尝试访问。但这些图像并没有存储到数据库中。但是当我使用log时,它显示插入所有值。但是我不能通过SQLItemManager在db中显示

代码:

按钮点击:

-(void)click:(id)sender{

    NSLog(@"click");

 NSString *databasePath=[[NSBundle mainBundle]pathForResource:@"db1" ofType:@"sqlite"];


    NSError *err=nil;
    NSFileManager *fm=[NSFileManager defaultManager];

    NSArray *arrPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, -1);
    NSString *path=[arrPaths objectAtIndex:0];
    NSString *path2= [path stringByAppendingPathComponent:@"db1.sqlite"];


     bool success=[fm copyItemAtPath:databasePath toPath:path2 error:&err];
    if(!success){
        NSLog(@"file copied successfully");
    }


    /*
    if(![fm fileExistsAtPath:path2])
    {

        bool success=[fm copyItemAtPath:databasePath toPath:path2 error:&err];
        if(success)
            NSLog(@"file copied successfully");
        else
            NSLog(@"file not copied");

    }
*/

    NSURL *URL = [NSURL URLWithString:@"http://myserver.net/projects/mobile/jsonstring.php"];



    NSError *error;
    NSString *stringFromFileAtURL = [[NSString alloc]
                                     initWithContentsOfURL:URL
                                     encoding:NSUTF8StringEncoding
                                     error:&error];

    //NSLog(@"response is %@", stringFromFileAtURL);

  //  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"db1.sqlite"];
    //NSLog(@"filepath %@",path);


    NSString *path1 = [path stringByAppendingPathComponent:@"db1.sqlite"];

    //NSDictionary *jsonDict = [stringFromFileAtURL JSONValue];


    //array

    NSArray *userData = [stringFromFileAtURL JSONValue];
    [stringFromFileAtURL release];

    int  i = 0;
    BOOL notExist = TRUE;
    sqlite3_stmt *statement, *addStmt;


    for (NSArray *skarray in userData) {
        //NSLog(@"test");


        if(i == 0){
            //insert all main category
            for (NSDictionary *tuser in skarray) {

                //write all this in the table
                //if already exists in data base id then overwrite the name

                //category table
                //NSLog(@"CategoryId is %@",[tuser objectForKey:@"cat_id"]);
                //NSLog(@"CategoryName is %@",[tuser objectForKey:@"cat_name"]);

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

                    const char *sql = [[NSString stringWithFormat:@"SELECT id FROM categories where id = '%@'",[tuser objectForKey:@"id"]] cStringUsingEncoding:NSUTF8StringEncoding];


                    NSLog(@"categories select is %s", sql);

                    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

                        notExist = TRUE;

                        while (sqlite3_step(statement) == SQLITE_ROW) {

                            notExist = FALSE;

                        }

                    }



                    if(notExist){
                        //NSLog(@"cat id does not exist");

                        const char *sqlInsert = [[NSString stringWithFormat:@"insert into categories (id,cat_name,order_by) values('%@','%@','%@')", [tuser objectForKey:@"id"], [tuser objectForKey:@"cat_name"],[tuser objectForKey:@"order_by"]] cStringUsingEncoding:NSUTF8StringEncoding];


                        NSLog(@"categories insert is %s", sqlInsert);

                        if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) == SQLITE_OK)
                            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

                        if(SQLITE_DONE == sqlite3_step(addStmt))



                            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));

                    }




                }



            }
        }

        if(i == 1){
            //insert all main category
            for (NSDictionary *tuser in skarray) {

                //write all this in the table
                //if already exists in data base id then overwrite the name

                //category table
                //NSLog(@"CategoryId is %@",[tuser objectForKey:@"cat_id"]);
                //NSLog(@"CategoryName is %@",[tuser objectForKey:@"cat_name"]);

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

                    const char *sql = [[NSString stringWithFormat:@"SELECT product_image FROM product where cat_id = '%@'",[tuser objectForKey:@"id"]] cStringUsingEncoding:NSUTF8StringEncoding];


                    // NSLog(@"product select is %s", sql);

                    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

                        notExist = TRUE;

                        while (sqlite3_step(statement) == SQLITE_ROW) {

                            notExist = FALSE;

                        }

                    }




                    if(notExist){
                        //NSLog(@"cat id does not exist");


                      //  imgData = UIImagePNGRepresentation([tuser objectForKey:@"product_image"]);

                        //  sqlite3_bind_blob(addStmt, 6, [imgData bytes], [imgData length], NULL);


                        const char *sqlInsert = [[NSString stringWithFormat:@"insert into product (id, cat_id,product_image,order_by) values ('%@','%@','%@','%@')", [tuser objectForKey:@"id"], [tuser objectForKey:@"cat_id"],[tuser objectForKey:@"product_image"],[tuser objectForKey:@"order_by"]] cStringUsingEncoding:NSUTF8StringEncoding];


                        NSLog(@"product insert %s", sqlInsert);

                        if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) == SQLITE_OK)
                            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));



                        if(SQLITE_DONE == sqlite3_step(addStmt))
                            NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));



                    }



                }



            }
        }



        if(i == 2){
            //insert all main category
            for (NSDictionary *tuser in skarray) {

                NSLog(@"version id is %@",[tuser objectForKey:@"product_id"]);

                const char *sqlInsert = [[NSString stringWithFormat:@"insert into version_app (id) values ('%@')", [tuser objectForKey:@"product_id"]] cStringUsingEncoding:NSUTF8StringEncoding];




                NSLog(@"stmt is %s", sqlInsert);

                if(sqlite3_prepare_v2(database, sqlInsert, -1, &addStmt, NULL) == SQLITE_OK)
                    NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

                if(SQLITE_DONE == sqlite3_step(addStmt))
                    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));


            }
        }

        i++;

    }

    }

你看到这里有什么不对劲吗:

 bool success=[fm copyItemAtPath:databasePath toPath:path2 error:&err];
if(!success){
    NSLog(@"file copied successfully");
}

如果数据库无法打开怎么办?@How Licks:如何检查数据库失败?请阅读规范。这就是为什么提供它。是的。我得到文件复制成功,而!成功案例。@hotlicks:我知道了。但数据并没有插入到数据库中。我正在获取所有值中的NSLog。但为什么不存储到数据库(db1.sqlite)file@user2674668-你看过说明书了吗???如果成功,copyItemAtPath将返回YES。如果返回值为“否”,则打印“文件复制成功”,然后根本不检查/打印错误值。阅读iOS接口的规范。SQLite的最新版本。Stack Overflow并不是一种其他人为您进行研究的服务。
 bool success=[fm copyItemAtPath:databasePath toPath:path2 error:&err];
if(!success){
    NSLog(@"file copied successfully");
}