我是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);
}
然后根据需要将数据添加到数据库中,它还将重复值添加到表中,因此对于Distinct Value我编写代码以获取数据,如
- (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的独特价值。请给我解决方案。我知道这很容易但我是数据库中的新手所以请抱歉 并提前致谢。
答案 0 :(得分:1)
这是因为尝试使用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
答案 1 :(得分:1)
您正在使用initWithUTF8String
从数据库中获取值,因此,如果数据库中有一个接受空数据的字段,那么在您尝试从数据库中获取空数据时会出现错误。
我建议您替换所有数据检索代码,如
NSString *postId = [[NSString alloc] initWithUTF8String: (const char *) sqlite3_column_text(statement, 1)];
以下代码:
NSString *postId = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
这会将非空数据替换为空字符串。