-1

我在我的服务器中存储了一些图像。并尝试从 URL 获取这些图像并存储到本地数据库,然后尝试访问。但是图像没有存储到数据库中。但是当我使用日志时,它会显示插入所有值。但我无法通过 SQLiteManager 在 db 中显示

代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    // [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];

    [btn setFrame:CGRectMake(50, 50, 30, 30)];

    [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];



}

按钮点击:

-(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++;

    }

    }
4

1 回答 1

0

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

 bool success=[fm copyItemAtPath:databasePath toPath:path2 error:&err];
if(!success){
    NSLog(@"file copied successfully");
}
于 2013-08-19T17:08:25.583 回答