插入查询不起作用(FMDB)
Insert Query not working (FMDB)
这是我用来将记录插入 sqlite 的代码 table
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;
}
我正在使用相同的逻辑从 table 获取数据,这对我来说效果很好。但是我不能插入记录。我不知道我在这里做错了什么。
我想我明白问题所在了。您在 运行 之后提交 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]);
The parameters must start with a colon. SQLite itself supports other
characters, but internally the Dictionary keys are prefixed with a
colon, do not include the colon in your dictionary keys.
因此这行代码必须重写为:
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
}
两个问题:
您正在呼叫 open
三次。一次在 openDatabase
中,两次在调用它的代码中。
所以,如果可以的话,让你的 open 例程打开数据库,但是 return 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;
}
然后检查结果:
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
.
FMResultSet *resultSet = [dataBase executeQuery:@"select * from CrewUnits"];
if ([resultSet next]) {
NSLog(@"ResultSet : %@", [resultSet resultDictionary]);
} else {
NSLog(@"No data found");
}
这是我用来将记录插入 sqlite 的代码 table
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;
}
我正在使用相同的逻辑从 table 获取数据,这对我来说效果很好。但是我不能插入记录。我不知道我在这里做错了什么。
我想我明白问题所在了。您在 运行 之后提交 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]);
The parameters must start with a colon. SQLite itself supports other characters, but internally the Dictionary keys are prefixed with a colon, do not include the colon in your dictionary keys.
因此这行代码必须重写为:
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
}
两个问题:
您正在呼叫
open
三次。一次在openDatabase
中,两次在调用它的代码中。所以,如果可以的话,让你的 open 例程打开数据库,但是 return
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; }
然后检查结果:
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
.FMResultSet *resultSet = [dataBase executeQuery:@"select * from CrewUnits"]; if ([resultSet next]) { NSLog(@"ResultSet : %@", [resultSet resultDictionary]); } else { NSLog(@"No data found"); }