我的应用程序从fmdatabase读取一些数据,并使用核心绘图库以图形方式显示它。我的应用程序第一次读取数据库时一切都很好,但问题是在将新数据插入数据库后我执行sql select语句并且它不会读取插入的新行。我不知道可能是什么问题,因为在我的应用程序执行插入后我在sqlite studio中执行相同的select语句并获取数据。
我已经读过,我必须将资源数据库复制到另一个文件夹,以便我可以读/写它,但也不起作用。
你有什么想法吗?
我的开场代码是这样的。
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];
答案 0 :(得分:0)
根据您现在提供的代码进行的一些观察。
检查返回值。
您应该检查executeUpdate
的del
和ins
返回值,如果其中一个是NO
,则应该检查lastErrorMessage
:
del = [db executeUpdate:queryUpdateDelete];
if (!del)
NSLog(@"%s: executeUpdate delete error: %@", __FUNCTION__, [db lastErrorMessage]);
ins = [db executeUpdate:queryUpdateInsert];
if (!ins)
NSLog(@"%s: executeUpdate insert error: %@", __FUNCTION__, [db lastErrorMessage]);
请勿使用stringWithFormat
。
你真的不应该使用stringWithFormat
来构建SQL,而是使用sql中的?
占位符,然后将参数传递给executeUpdate
:
queryUpdateInsert = @"INSERT INTO ZTESORERIACONSOLIDADO (Z_ENT, Z_OPT, ZANIOCORTE, ZMESCORTE, ZCONSUMOMESA, ZCONSUMOTOTAL, ZESTATUS, ZLINEA, ZLINEADISPONIBLE, ZCONTRAPARTE, ZIDCONTRAPARTE, ZINSTITUCION, ZMESA, ZTIPORIESGO) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
queryUpdateDelete = @"DELETE FROM ZTESORERIACONSOLIDADO WHERE ZMESCORTE = ? AND ZANIOCORTE = ?";
del = [db executeUpdate:queryUpdateDelete, @(zmescorte), @(zaniocorte-1)];
if (!del)
NSLog(@"%s: executeUpdate delete error: %@", __FUNCTION__, [db lastErrorMessage]);
ins = [db executeUpdate:queryUpdateInsert, @(z_ent), @(z_opt), @(zaniocorte), @(zmescorte), @(zconsumomesa), @(zconsumototal), @(zestatus), @(zlinea), @(zlineadisponible), zcontraparte, @(zidcontraparte), zinstitucion, zmesa, ztiporiesgo];
if (!ins)
NSLog(@"%s: executeUpdate insert error: %@", __FUNCTION__, [db lastErrorMessage]);
请注意用printf
SQL占位符替换?
样式格式化程序。
另请注意,executeUpdate
期望NSNumber
个对象而不是原始数字数据类型。因此,在上面的示例中,您将值存储在NSNumber
和double
变量中,我必须使用@()
来获取NSNumber
表示形式。更好的是,而不是从NSNumber
检索convertedData
,而不是将其转换为原始数据类型,例如NSInteger
或double
,然后转换回NSNumber
,只需传递从valueForKey
返回的原始对象,并完全丢失@()
。如果你这样做,你显然必须改变变量的数据类型,但这是一种更合乎逻辑的方法。)
关键的回家消息是,您希望通过使用executeUpdate
而不是{{}的参数来避免需要转义字符串中的任何特殊字符(以及保护自己免受SQL注入攻击)。 1}}。 (在stringWithFormat
中使用其他参数就像在SQLite库中使用executeUpdate
调用一样。)
这是一个CoreData数据库吗?
我是否从表名中的sqlite3_bind_xxx
推断出这是CoreData数据库的列名?如果是这样,除非您完全退出CoreData,否则我不建议通过SQLite直接与数据库进行交互(无论是通过FMDB还是Z
调用)。当然,如果您在尝试此FMDB代码时打开了CoreData,则可能会遇到问题(当您查看sqlite3_xxx
时会出现问题。)
但是,如果使用SQLite,请使用事务。
如果你想要优化你的算法(一旦你确定并修复了你的bug的来源),你可能想要在lastErrorMessage
调用中包含所有这些,因为如果你这样做的话很多记录,你可以获得显着的性能提升。