我昨天问过这个问题,但我没有回答,所以我想再问一遍。我的代码中的更新语句似乎不起作用。我在数据库中有值,每次使用update语句时,它都不会更新数据库。我已将sqlite数据库复制到我的设备,我正在引用它的路径。这是我用来复制数据库的代码
- (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;
}
这是我用来更新数据库的代码,但是虽然它确认它已经执行了sql命令(我在if语句中有了NSLogs,你将在下面看到)但它不会更新数据库。
-(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;
}
例如,此代码确认更新,但实际上并未发生更新
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]);
}