我的应用程序从 fmdatabase 读取一些数据并使用核心绘图库以图形方式显示它。我的应用程序第一次读取数据库时一切都很好,但问题是在将新数据插入数据库后,我执行了一个 sql 选择语句,它不会读取插入的新行。我不知道可能是什么问题,因为在 sqlite studio 中,我在我的应用程序执行插入后执行相同的选择语句并获取数据。
我已经读过我必须将资源数据库复制到另一个文件夹以便我可以读/写它,但也不起作用。
你有什么主意吗?
我的打开代码是这样的。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *writablePath = [docsPath stringByAppendingPathComponent:@"DatabaseName.sqlite"];
//this is the copy db from the resource database
FMDatabase *db = [FMDatabase databaseWithPath:writablePath];
这是执行插入和删除的代码。
if([db open]){
for (int i = 0; i < [updatedData count]; i++) {
z_ent = [((NSNumber *)([[updatedData objectAtIndex:i] valueForKey:@"ent"])) intValue];
z_opt = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"opt"]) intValue];
zaniocorte = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"anioCorte"]) intValue];
zmescorte = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"mesCorte"]) intValue];
zestatus = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"estatus"]) intValue];
zidcontraparte = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"idContraparte"]) intValue];
zconsumomesa = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"consumo"]) doubleValue];
zconsumototal = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"consumoTotal"]) doubleValue];
zlinea = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"linea"]) doubleValue];
zlineadisponible = [((NSNumber *)[[updatedData objectAtIndex:i] valueForKey:@"lineaDisponible"]) doubleValue];
zcontraparte = ((NSString *)[[updatedData objectAtIndex:i] valueForKey:@"contraparte"]);
zinstitucion = ((NSString *)[[updatedData objectAtIndex:i] valueForKey:@"institucion"]);
zmesa = ((NSString *)[[updatedData objectAtIndex:i] valueForKey:@"mesa"]);
ztiporiesgo = ((NSString *)[[updatedData objectAtIndex:i] valueForKey:@"tipoRiesgo"]);
queryUpdateInsert = [NSString stringWithFormat:@"INSERT INTO ZTESORERIACONSOLIDADO
(Z_ENT, Z_OPT, ZANIOCORTE, ZMESCORTE, ZCONSUMOMESA, ZCONSUMOTOTAL, ZESTATUS, ZLINEA, ZLINEADISPONIBLE, ZCONTRAPARTE, ZIDCONTRAPARTE, ZINSTITUCION, ZMESA, ZTIPORIESGO) VALUES (%d, %d, %d, %d, %f, %f, %f, %f, %f, '%@', %d, '%@', '%@', '%@');",z_ent, z_opt, zaniocorte, zmescorte, zconsumomesa, zconsumototal, zestatus, zlinea, zlineadisponible, zcontraparte, zidcontraparte, zinstitucion, zmesa, ztiporiesgo];
queryUpdateDelete = [NSString stringWithFormat:@"DELETE FROM ZTESORERIACONSOLIDADO WHERE ZMESCORTE = %d AND ZANIOCORTE = %d", zmescorte, zaniocorte-1];
del = [db executeUpdate:queryUpdateDelete];
ins = [db executeUpdate:queryUpdateInsert];
}
}
[db close];