从sqlite数据库中选择时无法获取数据,并显示[nsplaceholderString initwithutf8string:]:null cstring'的错误。
...我提到执行此操作的所有步骤。因此,如果我在某种过程中我错了,请纠正我。
我已经从sqlite manager创建了数据库。然后,我将.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函数,在步骤函数显示null字符串的错误,就好像数据没有从数据库中获取任何内容一样。
所以在这里,我在做什么错?在实施数据库过程的某个地方,我是否错了?
以及如何将数据插入数据库中?我的意思是,如果我们将更新的数据库打开到SQLite Manager中,它会显示其中的更新数据吗?
请帮助我清除我的疑问。
您发布的代码从捆绑包中复制一个SQL文件,插入条目,然后在打开它之前再次从捆绑包中复制原始SQL文件并尝试阅读新添加的条目。课程没有添加任何添加。您使用未添加任何内容的启动文件覆盖文件。