我在iPhone上使用iOS 4中的SQLite,但我的更新語句所做的更改未保存。起初以爲退出應用程序可能會以某種方式刪除數據庫,但在同一會話期間甚至不會保留。初始化我的數據庫的代碼是(使用FMDB):未保存SQLite更改
-(SQLiteDal*) init {
pool = [[NSAutoreleasePool alloc] init];
self = [super init];
if(self != nil){
// Setup some globals
NSString *databaseName = [self getDbPath];
NSLog([NSString stringWithFormat:@"db path: %@", databaseName]);
db = (FMDatabase *)[FMDatabase databaseWithPath:databaseName];
if (![db open]) {
NSLog(@"Could not open db.");
[pool release];
return 0;
}
}
//[self checkAndCreateDatabase];
return self;
}
#pragma mark DB Maintenance
-(NSString *)getDbPath {
NSString *databaseName = @"myapp.db";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databaseName = [documentsDir stringByAppendingPathComponent:databaseName];
return databaseName;
}
這些方法被稱爲創建數據庫Both,然後插入到表我打電話:
[db executeQuery:@"INSERT INTO MyTable (Name, Description, Area, Price, ID) VALUES (?,?,?,?,?)",
f.Name,
f.description,
f.area,
f.price,
f.id];
問題是,當我用下面的語句來自MyTable
閱讀,我從來沒有得到任何東西:
FMResultSet *rs = [db executeQuery:@"SELECT * FROM MyTable WHERE ID = ?", id];
while ([rs next]) {
//.. this is never called
至於我可以看到我不丟失任何東西,而數據庫似乎處於可寫位置。
具有u打開烏爾分貝sqlite的經理ñ檢查插入後保存的記錄? – booleanBoy
不,數據庫在手機上。 – Echilon
使用NSString stringWithFormat同時將查詢傳遞給executeQuery方法.....並且還將其轉換爲UTF8String ...只是試試 – booleanBoy