我在更新数据库时遇到问题。我相信这是由于它可能是只读的,但我不确定。我在我的控制台中创建了我的数据库,下面的代码将数据库添加到项目文件夹,而不是在外部引用它。下面是对文件传输到项目文件夹无法更新数据库
- (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