插入查询不起作用(FMDB)

时间:2015-06-05 15:27:56

标签: ios sqlite fmdb

以下是我用来将记录插入sqlite表的代码

FMDatabase *dataBase = [self openDatabase];
    [dataBase open];
    if ([dataBase open] != YES) {
        NSLog(@"DB Error %d: %@", [dataBase lastErrorCode], [dataBase lastErrorMessage]);
    //VERY IMPORTANT
    }

    BOOL success= [dataBase executeUpdate:@"Insert into CrewUnits (pkCrewUnits,fkUnits,fkAgencyVehicles,fkLookupCodes_PrimaryRole, fkLookupCodes_LevelOfCare, PrimaryUnit) values (?, ?, ?, ?, ?, ?);", pkCrewUnits, fkUnits, fkAgencyVehicle, fkLookupCodes_PrimaryRole, fkLookupCodes_LevelOfCare, [NSNumber numberWithInt:1]];
    NSLog(success ?@"YES" :@"NO");
     NSLog(@"Error %d: %@", [dataBase lastErrorCode], [dataBase lastErrorMessage]);
   FMResultSet*resultSet= [dataBase executeQuery:@"select * from CrewUnits"];
    NSLog(@"ResultSet : %@", [resultSet resultDictionary]);

    [dataBase close];

数据库路径

- (FMDatabase *)openDatabase
{

    NSLog(@"Open Database");
    NSString *documents_dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *db_path = [documents_dir stringByAppendingPathComponent:[NSString stringWithFormat:@"HPSix_05BD.db"]];    // DatabasePath
    FMDatabase *db = [FMDatabase databaseWithPath:db_path];
    if (![db open])
        NSLog(@"Failed to open database!!!!!");
    return db;
}

我使用相同的逻辑从表中获取数据对我来说很好。但是我无法插入记录。我不知道我在这里做错了什么。

2 个答案:

答案 0 :(得分:0)

我想我看到了问题。您在运行SELECT查询后正在提交。

您必须在UPDATE查询后立即提交。当您在选择时就像现在一样,UPDATE查询尚未提交,因此不会返回。

[dataBase beginTransaction];
BOOL success= [dataBase executeUpdate:@"Insert into CrewUnits (pkCrewUnits,fkUnits,fkAgencyVehicles,fkLookupCodes_PrimaryRole, fkLookupCodes_LevelOfCare, PrimaryUnit) values (?, ?, ?, ?, ?, ?);", pkCrewUnits, fkUnits, fkAgencyVehicle, fkLookupCodes_PrimaryRole, fkLookupCodes_LevelOfCare, [NSNumber numberWithInt:1]];
[dataBase commit]; // commit the query
NSLog(success ?@"YES" :@"NO");
NSLog(@"Error %d: %@", [dataBase lastErrorCode], [dataBase lastErrorMessage]);
// now at this point the transaction has been committed and can be selected
FMResultSet*resultSet= [dataBase executeQuery:@"select * from CrewUnits"];
NSLog(@"ResultSet : %@", [resultSet resultDictionary]);

另外according to FMDB docs

  

参数必须以冒号开头。 SQLite本身支持其他   字符,但在内部字典键前缀为   冒号,不要在字典键中包含冒号。

因此必须将这行代码重写为:

BOOL success= [dataBase executeUpdate:@"Insert into CrewUnits (pkCrewUnits,fkUnits,fkAgencyVehicles,fkLookupCodes_PrimaryRole, fkLookupCodes_LevelOfCare, PrimaryUnit) values (:pkCrewUnits,:fkUnits,:fkAgencyVehicles,:fkLookupCodes_PrimaryRole, :fkLookupCodes_LevelOfCare, :PrimaryUnit);", pkCrewUnits, fkUnits, fkAgencyVehicle, fkLookupCodes_PrimaryRole, fkLookupCodes_LevelOfCare, [NSNumber numberWithInt:1]];

最后根据您使用next的文档循环搜索结果:

while ([resultSet next]) {
    //retrieve values for each record
}

答案 1 :(得分:0)

两个问题:

  1. 您正在呼叫par(mfrow = c(3,3)) for (i in 1:55) { matplot(t(rlkandsig[c(i,55),2:5]), type="l", pch=20, lty=1, xlab="time", ylab="ctrl_lsm1_ratio") points(d2[c(i,55,(i+55),110,(i+110),165,(i+165),220),1], d2[c(i,55,(i+55),110,(i+110),165,(i+165),220),4], col=ifelse(d2[c(i,55,(i+55),110,(i+110), 165,(i+165),220),2]=="yes",1,2),cex=2.0) legend("topright",legend=c("yes","no"), col=c(1,2), pch=21) if (i >9 & i%%9==0) { quartz() par(mfrow = c(3,3)) } } 三次。进入open后两次调用代码。

    所以,如果可以的话,让你的开放例程打开数据库,但如果不能,则返回openDatabase

    nil

    然后检查结果:

    - (FMDatabase *)openDatabase
    {
        NSLog(@"Open Database");
        NSString *documents_dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *db_path = [documents_dir stringByAppendingPathComponent:[NSString stringWithFormat:@"HPSix_05BD.db"]];    // DatabasePath
        FMDatabase *db = [FMDatabase databaseWithPath:db_path];
        if (![db open]) {
            NSLog(@"Failed to open database!!!!!");
            return nil;
        }
        return db;
    }
    
  2. 致电FMDatabase *dataBase = [self openDatabase]; if (!dataBase) { // quit; note, no point in checking error message as it only returns meaningful messages if you have an open database } 后,您必须致电executeQuery

    next