gpt4 book ai didi

ios - 如何在 iOS 中显示来自 Sqlite 表的不同值?

转载 作者:行者123 更新时间:2023-11-28 21:52:14 26 4
gpt4 key购买 nike

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

我将数据添加到我的 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);
}

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

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

然后它像 as 一样给我错误

[NSPlaceholderString initWithUTF8String:]: NULL cString

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

最佳答案

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

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

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

使用以下代码:

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

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

关于ios - 如何在 iOS 中显示来自 Sqlite 表的不同值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27896842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com