2013-08-19 114 views
-1

我在服務器中存儲了一些圖像。並嘗試從URL獲取這些圖像並存儲到本地數據庫,然後嘗試訪問。但圖像不存儲到數據庫。但是當我使用日誌顯示插入所有的值。但我可以在DB通過的SQLiteManager未顯示未存儲到數據庫的圖像

代碼:

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

    } 

    } 
+0

如果會發生什麼數據庫無法打開? –

+0

@ How Licks:如何檢查DB失敗? – user2674668

+0

閱讀規格。這就是它提供的原因。 –

回答

0

你看這裏有什麼問題:

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

大聲笑很好的答案 –

+0

是的。我成功的情況下成功複製文件! – user2674668

+0

@ user2674668 - 呃,閱讀規範。 –