2015-01-12 3 views
0

저는 iOS 개발에 초보자입니다. Sqllite 데이터베이스를 처음 사용하십시오.이 질문에 대해서는 유감이지만 너무 혼란 스럽습니다. 해결책을주세요. iOS의 Sqllite 테이블에서 다른 값을 표시하는 방법?

나는

그리고 내가 원하는과 고유 가치를 위해 내가 쓰기 때문에 그것은 또한, 테이블에서 중복 값을 추가로 데이터베이스에 데이터를 추가
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
sqlite3_stmt *statement; 
const char *dbpath = [_databasePath UTF8String]; 

if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK) { 

    NSString *insertSQL = [NSString stringWithFormat: 
          @"INSERT INTO ALLREADTABLE (PostId, PostTitle, ImageLink, ShortDescription,Category,PostDate) VALUES (\"%@\", \"%@\", \"%@\", \"%@\",\"%@\",\"%@\")", 
          post_id, cell.headLabel.text, image,self.shortDiscription,cell.categoryLabel.text,cell.timeLabel.text]; 

    const char *insert_stmt = [insertSQL UTF8String]; 
    sqlite3_prepare_v2(_myDatabase, insert_stmt, 
         -1, &statement, NULL); 
    if (sqlite3_step(statement) == SQLITE_DONE) 
    { 
     NSString *string=[NSString stringWithFormat:@"Value Added %@ %@ %@ %@ %@ %@",post_id, cell.headLabel.text, image,self.shortDiscription,cell.categoryLabel.text,cell.timeLabel.text]; 
     NSLog(@"String %@",string); 
    } else 
    { 
     NSLog(@"Failed to add contact"); 
    } 
    sqlite3_finalize(statement); 
    sqlite3_close(_myDatabase); 
} 

로 내 Sqllite 표 등으로 데이터를 추가 코드는

[NSPlaceholderString initWithUTF8String:]: NULL cString 

로 H처럼 나에게 오류를주고 그 다음
- (void)getTextFomDB 
{ 
NSString *docsDir; 
NSArray *dirPaths; 

// Get the documents directory 
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsDir = dirPaths[0]; 

// Build the path to the database file 
_databasePath = [[NSString alloc] 
       initWithString: [docsDir stringByAppendingPathComponent: 
           @"sampleDatabase.db"]]; 

const char *dbpath = [_databasePath UTF8String]; 
sqlite3_stmt *statement; 

if (sqlite3_open(dbpath, &_myDatabase) == SQLITE_OK) 
{ 
    NSString *querySQL = @"SELECT DISTINCT PostId, PostTitle, ImageLink, ShortDescription,Category,PostDate FROM ALLREADTABLE"; 

    const char *query_stmt = [querySQL UTF8String]; 

    if (sqlite3_prepare_v2(_myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK) 
    { 
     [self.postId removeAllObjects]; 
     [self.postTitle removeAllObjects]; 
     [self.imageLink removeAllObjects]; 
     [self.shortDecsription removeAllObjects]; 
     [self.category removeAllObjects]; 
     [self.postDate removeAllObjects]; 
     while (sqlite3_step(statement) == SQLITE_ROW) { 
      //NSString *personID = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 0)]; 
      NSString *postId = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)]; 
      NSString *postTitle = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 2)]; 
      NSString *ImageLink = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 3)]; 
      NSString *shortDescrip = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 4)]; 
      NSString *Category = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 5)]; 
      NSString *postDate = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 6)]; 
      [self.postId addObject:[NSString stringWithFormat:@"%@ ", postId]]; 
      [self.postTitle addObject:[NSString stringWithFormat:@"%@ ",postTitle]]; 
      [self.imageLink addObject:[NSString stringWithFormat:@"%@",ImageLink]]; 
      [self.shortDecsription addObject:[NSString stringWithFormat:@" %@",shortDescrip]]; 
      [self.category addObject:[NSString stringWithFormat:@" %@",Category]]; 
      [self.postDate addObject:[NSString stringWithFormat:@" %@",postDate]]; 
     } 
     sqlite3_finalize(statement); 
    } 
    sqlite3_close(_myDatabase); 
} 
} 

으로 같은 데이터를 가져 내 SqlliteTable에서 고유 한 값을 원하기 때문에? 제발 해결책을주세요. 나는 그것이 쉽다는 것을 알고있다 그러나 나는 데이터베이스에있는 초보자이다. 그래서 미안하다 그리고 사전에 감사합니다.

+0

왜이 태그가 MySQL로 태그 되나요? – shmosel

답변

1

데이터베이스에서 값을 가져 오는 데 initWithUTF8String을 사용하고 있으므로 데이터베이스에서 null 데이터를 가져 오는 필드가 있으면 데이터베이스에서 Null 데이터를 가져 오는 중 오류가 발생합니다.

난 당신이 코드를 아래로

NSString *postId = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)]; 

과 같은 코드를 검색하는 모든 데이터를 대체 제안 :

NSString *postId = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(statement, 1)]; 

이 빈 문자열로 널이 아닌 데이터를 대체합니다.

1

이것은 NULL 문자열을 사용하여 NSString 개체를 만들려고하여 발생합니다.

char *tmp = sqlite3_column_text(statement, 1); 
if (tmp == NULL) 
    postId = nil; 
else 
    postId = [[NSString alloc] initWithUTF8String:tmp]; 

참조 : lnafziger

당신은 SQL 문의 결과와는 NSString을 만들기 전에이 같은 NULL을 확인해야

[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, ...)]; 

그래서 : 그것은이 라인 중 하나입니다

관련 문제