2013-05-21 127 views
0

我在向表中插入值的数组时遇到问题..我必须将联系人详细信息的数组插入表中。也就是说,我已经列出了第一个在一个变量,在变量姓氏列表等名称..我同样要被插入24列..将值的数组插入到表中

它抛出一个异常说:

***终止由于应用未找到异常“NSInvalidArgumentException”,原因:' - [contactlist filepath]:无法识别的选择器发送到实例0xa2af5b0'***第一次调用堆栈:(0x257e012 0x1e06e7e 0x26094bd 0x256dbbc 0x256d94e 0x4b613 0x4b551 0x190c589 0x190a652 0x190b89a 0x190a6 0D 0x190a785 0x1857a68 0x4a0911 0x49fbb3 0x4ddcda 0x25208fd 0x4de35c 0x4de2d5 0x3c8250 0x2501f3f 0x250196f 0x2524734 0x2523f44 0x2523e1b 0x2ad57e3 0x2ad5668 0xd4affc 0x254d 0x2475)的libC++ abi.dylib:终止叫做抛出异常

谁能帮助我走出这个..?

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    NSError *error; 
    json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 
    NSLog(@"json.... %@",json); 


    id jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil]; 

    NSLog(@"jsonObject=%@", jsonObject); 

    NSArray *checkArray=[jsonObject valueForKey:@"ND"]; 



    cheDisk=[checkArray valueForKey:@"FN"]; 
    cheDisk1=[checkArray valueForKey:@"LN"]; 


    NSLog(@"FN =%@",cheDisk); 
    NSLog(@"LN =%@",cheDisk1); 

    fname = [NSString stringWithFormat:@"%@",[cheDisk objectAtIndex:0]]; 
    lname = [NSString stringWithFormat:@"%@",[cheDisk1 objectAtIndex:0]]; 

    [self insertData]; 

    [ContactTableview reloadData]; 

} 

-(void)insertData 

{ 
    if (sqlite3_open([[self filepath]UTF8String], &db) == SQLITE_OK) 
    { 

     NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO pu_Contacts (FirstName, LastName) VALUES ('%@', '%@')", fname, lname]; 

     char *error; 
     sqlite3_exec(db, [insertStatement UTF8String], NULL, NULL, &error)!= SQLITE_OK; 

     sqlite3_close(db); 

    } 
} 

+(NSString *) filepath { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    return [documentsDir stringByAppendingPathComponent:@"UWdatabase1.sql"]; 
} 
+0

那么,问题是什么,'fname'和'lname'没有插入表中? 'char * error'说什么? 'NSLog'来看看发生了什么。 – Malloc

+1

Does * contactlist *有一个叫做'filepath'的方法吗? – Larme

+0

是的..要获取文件路径..我只是没有粘贴在这里,所以减少代码.. – Mano

回答

0

问题是与此代码:

[self filepath] 

您定义的方法为class method,因此使用self你不能调用它。

+(NSString *) filepath 

解决方案1:

更改使用类名像[contactlist filepath];呼叫。

解决方案2:

更改方法实例方法,如:

- (NSString *) filepath 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 
    return [documentsDir stringByAppendingPathComponent:@"UWdatabase1.sql"]; 
} 
+0

虽然它不工作..任何人都可以提出自己的代码来实现这个逻辑 – Mano

+0

@ManojEllappan:现在是什么问题?你尝试过两种解决方案吗? –

0

什么类的名字?按类名更改self以调用类方法: if (sqlite3_open([[MyClassName filepath]UTF8String], &db) == SQLITE_OK)