如何在 iOS 中显示来自 Sqlite Table 的不同值?

How to Show Distinct Value From Sqllite Table in iOS?

我是 iOS 开发的新手,第一次使用 Sqllite 数据库,很抱歉这个问题,但我很困惑请给我解决方案。

我将数据添加到 My Sqllite Table 就像

-(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);
}

然后根据需要将数据添加到数据库中,并且还将重复值添加到 table 中,因此对于不同的值,我编写代码来获取数据,如 as

- (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);
}
}

然后它给我错误

[NSPlaceholderString initWithUTF8String:]: NULL cString

这里我想要来自我的 SqlliteTable 的不同值。请给我解决方案。我知道这很容易但是我是数据库的新手所以请抱歉 提前致谢。

这是由于试图用 NULL 字符串创建 NSString 对象造成的。它位于以下行之一:

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

因此,在使用 sql 语句的结果创建 NSString 之前,您需要像这样检查 NULL:

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

参考:lnafziger

您正在使用 initWithUTF8String 从数据库中获取值,因此,如果数据库中有一个字段接受空数据,那么当您尝试从数据库中获取空数据时,它会给您一个错误。

我建议您替换所有数据检索代码,如

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

使用以下代码:

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

这会将非空数据替换为空字符串。