数据,并显示[NSPlaceholderString initWithUTF8String:]的错误:NULL cString' ,在步骤功能。
... 我提到了我执行此操作所遵循的所有步骤。如果我在某些程序上错了,请纠正我。
我已经在sqlite管理器中创建了带有4列(N_id,N_Title,N_Desc,N_img)的表news_array中的database.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管理器中,它是否会在其中显示更新的数据?
请帮助我清除疑虑。答案 0 :(得分:1)
您发布的代码从捆绑包中复制SQL文件,插入一个条目,然后再打开该捆绑包中的原始SQL文件,然后再打开它并尝试读取新添加的条目。 当然没有添加任何内容。您使用没有添加任何内容的起始文件覆盖了您的文件。