【问题标题】:data could not be fetched when selected from sqlite database从 sqlite 数据库中选择时无法获取数据
【发布时间】:2016-11-26 22:24:53
【问题描述】:

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

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

我已经从 sqlite 管理器创建了 database.sqlite 文件,在带有 4 列(N_id、N_Title、N_Desc、N_img)的表 news_array 内并保存了它。然后我将 .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 个数组对象)在 step 函数成功时都会收到 NSLog 消息。

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

所以在这里,我做错了什么? 我在某处实现数据库过程错了吗?

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

请帮我解开疑惑。

【问题讨论】:

    标签: ios objective-c sqlite ios10 sqlitemanager


    【解决方案1】:

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

    【讨论】:

    • 好的。我明白你想说什么或问题是什么。所以我现在应该在这里做什么。我第一次实时实施 sqlite。我遵循了许多教程,但找不到完美的程序。根据您的说法,我应该遵循哪个代码程序?你能指导我吗?有什么参考吗?或任何链接?或任何代码?你有什么。
    • 按照你的建议,我已经相应地更改了代码,
    • // 获取文档目录 NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; // 构建数据库文件路径 self.databasePath = [documentsDir stringByAppendingPathComponent:@"Database.sqlite"];
    • if (!databaseAlreadyExists) { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *databasePathFromApp = [ [ [NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Database.sqlite"]; if ( ![ fileManager fileExistsAtPath:databasePathFromApp ] ) { NSLog(@"文件在路径中不存在");返回; } [ fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil ]; NSLog(@"创建的数据库"); }
    • title 是第二列。所以据此, NSString *title = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(compiledStatement, 2)]; ..但我仍然在这行代码中遇到与空字符串相同的错误。
    猜你喜欢
    • 2018-05-28
    • 2012-01-06
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多