我在更新數據庫時遇到問題。我相信這是由於它可能是隻讀的,但我不確定。我在我的控制檯中創建了我的數據庫,下面的代碼將數據庫添加到項目文件夾,而不是在外部引用它。下面是對文件傳輸到項目文件夾無法更新數據庫
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
databaseName = @"databasenew.sql";
/* copy over to phone code */
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];
NSString *sqlFile =[documentDirectory stringByAppendingPathComponent:@"databasenew.sql"];
if (![[NSFileManager defaultManager] fileExistsAtPath:sqlFile]) {
NSString *bundleSql = [[NSBundle mainBundle] pathForResource:@"databasenew" ofType:@"sql"];
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:bundleSql toPath:sqlFile error:&error];
if (error) {
NSLog(@"Could not copy json file: %@", error);
}
else{
NSLog(@"yea");
}
}
else{
NSLog(@"transfer complete");
}
NSString *bundleSql = [[NSBundle mainBundle] pathForResource:@"databasenew" ofType:@"sql"];
databasePath = bundleSql;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
這裏是我嘗試更新
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"Message"])
{
NSLog(soapResults);
NSString *convertString = soapResults;
check = [convertString intValue];
NSLog(@"user is");
NSLog(user);
if(check > 0){
databaseName = @"databasenew.sql";
NSString *bundleSql = [[NSBundle mainBundle] pathForResource:@"databasenew" ofType:@"sql"];
databasePath = bundleSql;
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "UPDATE people set user = 'munch'";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSLog(@"success");
NSLog(soapResults);
BOOL success = sqlite3_step(compiledStatement);
if (success != SQLITE_DONE) {
BOOL myBool=YES;
NSLog([NSString stringWithFormat:@"My Bool value : %d",success]);
}
}else{
NSLog(@"the fail ispreparing something");
NSAssert1(0, @"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}else{
NSLog(@"fail");
}
sqlite3_close(database);
NSString *string = [NSString stringWithFormat:@"%d", check];
NSLog(string);
NSLog(@"lolololol");
check = 0;
[self Bootup];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Problem!" message:@"Incorrect Username or Password"
delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Yes", nil];
[alert show];
[alert release];
}
// gotoContent = checkConfirm;
}
if([elementName isEqualToString:@"ValidateUserResponse"])
{
if(!soapResults)
{
soapResults = [[NSMutableString alloc] init];
NSLog(soapResults);
NSLog(@"above is soap validate user");
}
recordResults = TRUE;
}
}
我已經做了代碼的代碼我NDlog調試,如果更新代碼成功,它會顯示「成功」,但數據庫本身不會更新。這是什麼原因?
能詳細你這是什麼意思數據庫沒有更新出來。 – Robin
所以我的默認值(我只在該coloum中有一個)是raji,當我使用update語句時,甚至知道它說成功地將raji更新爲munch,當我重新打開應用程序時,或者調用該列時仍然是raji。因此,不知何故,我的更新語句實際上並未實際更新數據庫(或表)。 –
你能告訴你哪一行是指你說的成功嗎 – Robin