Ios 如何从FML数据库中读取新插入的数据

Ios 如何从FML数据库中读取新插入的数据,ios,objective-c,sqlite,fmdb,Ios,Objective C,Sqlite,Fmdb,我的应用程序从FMB数据库中读取一些数据,并使用core plot library以图形方式显示。当我的应用程序第一次读取数据库时,一切都很好,但问题是在将新数据插入数据库后,我执行了一个sql select语句,但它不会读取插入的新行。我不知道会出现什么问题,因为在sqlite studio中,在我的应用程序进行插入并获取数据之后,我会执行相同的select语句 我已经读到,我必须将一个资源数据库复制到另一个文件夹,这样我才能读/写它,但也不能工作 你知道吗 我的开场白是这样的 NSArray

我的应用程序从FMB数据库中读取一些数据,并使用core plot library以图形方式显示。当我的应用程序第一次读取数据库时,一切都很好,但问题是在将新数据插入数据库后,我执行了一个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];
if([db open]){
对于(int i=0;i<[UpdateData count];i++){
z_ent=[((NSNumber*)([[updatedata objectAtIndex:i]valueForKey:@“ent”])intValue];
z_opt=[((NSNumber*)[[updatedata objectAtIndex:i]valueForKey:@“opt”])intValue];
zaniocorte=[((NSNumber*)[[UpdateData objectAtIndex:i]valueForKey:@“anioCorte”])intValue];
zmcardate=[((NSNumber*)[[updatedata objectAtIndex:i]valueForKey:@“mesCorte”])intValue];
zestatus=[((NSNumber*)[[UpdateData objectAtIndex:i]valueForKey:@“estatus”])intValue];
zidContractParte=[((NSNumber*)[[UpdateData objectAtIndex:i]valueForKey:@“idContractParte”])intValue];
zconsumomesa=[((NSNumber*)[[updatedata objectAtIndex:i]valueForKey:@“consumo”])doubleValue];
zconsumototal=[((NSNumber*)[[updatedata objectAtIndex:i]valueForKey:@“consumoTotal”])doubleValue];
zlinea=[((NSNumber*)[[updatedata objectAtIndex:i]valueForKey:@“linea”])doubleValue];
zlineadisponsible=[((NSNumber*)[[UpdateData objectAtIndex:i]valueForKey:@“lineadisponsible”])doubleValue];
zcontraparte=((NSString*)[[UpdateData objectAtIndex:i]valueForKey:@“相反方]);
zinstitucion=((NSString*)[[UpdateData objectAtIndex:i]valueForKey:@“institucion”]);
zmesa=((NSString*)[[updatedata objectAtIndex:i]valueForKey:@“mesa]”);
ztiporiesgo=((NSString*)[[updatedData objectAtIndex:i]valueForKey:@“tipoRiesgo]”);
queryUpdateInsert=[NSString stringWithFormat:@”插入ZTESORERIACONSOLIDADO
(Z_ENT,Z_OPT,ZANIOCORTE,Zmconsumomesa,ZCONSUMOTOTAL,ZESTATUS,ZLINEA,Zlineadible,ZCONTRAPARTE,Zincontraparte,ZINSTITUCION,ZMESA,ZTIPORIESGO)值(%d,%d,%d,%f,%f,%f,'%f','%@','%d','%d','%d','%@';”,z_ent,z_opt,zaniocorte,Zmconsumomesa,zconsumototal,zestatus,zlinea,zlinea负责,zcontraparte,Zintoparate,zinstitucion,zmesa,ztiporiesgo];
queryUpdateDelete=[NSString stringWithFormat:@“从ZTESORERIACONSOLIDADO中删除,其中ZMCHARDESTE=%d和ZANIOCORTE=%d”,ZMCHARDESTE,ZANIOCORTE-1];
del=[db executeUpdate:queryUpdateDelete];
ins=[db executeUpdate:queryUpdateInsert];
}
}
[db close];

基于您现在提供的代码的一些观察结果

  • 检查返回值

    您应该检查
    del
    ins
    返回值,如果其中一个是
    NO
    ,您应该检查:

  • 不要使用
    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
    表示。更好的是,与其从
    convertedData
    中检索
    NSNumber
    ,将其转换为原始数据类型,如
    NSInteger
    double
    ,然后再转换回
    NSNumber
    ,只需传递从
    valueForKey
    返回的原始对象,并丢失
    @()
    总共。如果这样做,显然必须更改变量的数据类型,但这是一种更符合逻辑的方法。)

    不过,关键的要点是,您希望通过使用参数
    executeUpdate
    而不是
    stringWithFormat
    来避免转义字符串中的任何特殊字符(以及保护自己免受SQL注入攻击)。(在
    executeUpdate
    中使用附加参数就像在SQLite库中使用
    sqlite3\u bind\u xxx
    调用一样。)

  • 这是CoreData数据库吗

    我是否从表名和列名中的
    Z
    推断这是一个CoreData数据库?如果是这样的话,除非您已经完全退出CoreData,否则我不会建议您通过SQLite直接与数据库交互(无论是通过FMDB还是
    sqlite3_xxx
    调用)。当然,如果您在尝试此FMDB代码时打开了CoreData,则可能会出现问题(当您查看
    lastErrorMessage
    时,这些问题将被点亮)

  • 但是,如果使用SQLite,请使用事务

    如果您想改进算法(一旦您确定并修复了bug的来源),您可能希望将所有这些都封装在
    内部事务
    调用中,因为如果您使用大量记录进行此操作,您可以获得显著的性能增益


  • 插入后是否关闭光标?
    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]);