从 sqlite 数据库中选择时无法获取数据,并在 step function 中显示 [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 中打开更新的数据库,它会在其中显示更新的数据吗?
请帮我解开我的疑惑。