从 sqlite 数据库中选择时无法获取数据

data could not be fetched when selected from sqlite database

从 sqlite 数据库中选择时无法获取数据,并在步骤函数中显示 [NSPlaceholderString initWithUTF8String:]: NULL cString' 错误。

... 我提到了执行此操作所遵循的所有步骤。如果我在某些程序上错了,请纠正我。

我创建了 database.sqlite 文件,在 table news_array 中有 4 列(N_id,N_Title,N_Desc,N_img ),来自 sqlite 管理器并保存它。然后我将 .sqlite 文件拖放到项目中。添加 libsqlite3.tbd 并导入 sqlite3.h .

然后在 .m 文件中,我创建了插入操作并尝试将数组数据插入数据库。

-(void)insert
NSArray *documentPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString *documentsDir = [documentPaths objectAtIndex:0];

NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"Database.sqlite"];

NSString *databasePathFromApp = [ [ [NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Database.sqlite"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ( ![ fileManager fileExistsAtPath:databasePathFromApp ] )

    return;

[ fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil ];

sqlite3 *database;

if (sqlite3_open( [databasePath UTF8String], &database) == SQLITE_OK)
{
    for (int i = 0; i < 10 ; i++)
    {
 NSString * nwsid = [[arrydata_tbl objectAtIndex:i]objectForKey:@"NewsId"];
int myInt = [nwsid intValue];
NSString * nwsttl = [[arrydata_tbl objectAtIndex:i]objectForKey:@"NewsTitle"];
NSString * nwsdesc = [[arrydata_tbl objectAtIndex:i]objectForKey:@"NewsDescription"];
        NSData * nwsimg = [[arrydata_tbl objectAtIndex:i]objectForKey:@"NewsImage"];

         const char *insert_stmt ="INSERT INTO news_array (N_id, N_Title , N_Desc ,N_img )VALUES (?,?,?,?);";
        sqlite3_stmt *compiledStatement;

        sqlite3_prepare_v2(database, insert_stmt,-1, &compiledStatement, NULL) ;

        {
            sqlite3_bind_int(compiledStatement, 1, myInt);
sqlite3_bind_text(compiledStatement, 2, [nwsttl UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 3, [nwsdesc UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_blob (compiledStatement, 4, [nwsimg bytes], (int)[nwsimg length], SQLITE_TRANSIENT);

if (sqlite3_step(compiledStatement) == SQLITE_DONE)

        {
            NSLog(@"Data inserted ...");
        }
             sqlite3_reset(compiledStatement);
    }
        sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
[self showData];
}

然后我从数据库中选择一列来查看是否插入了数据,

-(void)showData
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDir = [documentPaths objectAtIndex:0];

NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"Database.sqlite"];

NSString *databasePathFromApp = [ [ [NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Database.sqlite"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ( ![ fileManager fileExistsAtPath:databasePathFromApp ] )

    return;

[ fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil ];

sqlite3 * database;




NSMutableArray * arry_ttl = [[NSMutableArray alloc]init];

if (sqlite3_open( [databasePath UTF8String], &database) == SQLITE_OK)
{
    NSString * insertSQL = [NSString stringWithFormat:@"SELECT N_Title FROM news_array"];

    const char *insert_stmt = [insertSQL UTF8String];
     sqlite3_stmt *compiledStatement;

    if(sqlite3_prepare_v2(database, insert_stmt,-1, &compiledStatement, NULL)==SQLITE_OK)
    {
    while (sqlite3_step(compiledStatement) == SQLITE_ROW)
    {
        NSString *title = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(compiledStatement, 1)];

        NSMutableDictionary *dic = [NSMutableDictionary new];

        [dic setObject:title forKey:@"title"];



        if(![arry_ttl containsObject:dic])
        {
            [arry_ttl addObject:dic];
           // [ary_ids addObject:id1];
        }
    }
        sqlite3_reset(compiledStatement);
 }
     sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}

我检查了插入时的程序,每次(每个循环有 10 个数组对象)在步进函数成功时收到 NSLog 消息。

但在 -(void)showdata 函数中,在步骤函数中显示空字符串错误,就好像数据没有从数据库中获取任何信息一样。

那么在这里,我做错了什么? 我在某个地方执行数据库过程错了吗?

以及如何从外部检查我们的数据是否已插入数据库? 我的意思是如果我们将更新的数据库打开到 sqlite 管理器中,它会显示其中的更新数据吗?

请帮我解开疑惑。

您发布的代码从包中复制了一个 SQL 文件,插入一个条目,然后再次从包中复制原始 SQL 文件,然后再打开它并尝试读取新添加的文件入口。 课程 没有添加任何内容。您用未添加任何内容的起始文件覆盖了您的文件。