我试图在Xcode中更新我的sqlite数据库中的一些数据,但是它一直在失败。这是我的代码。
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"users.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)
{
NSLog(@"Error: Cant find users db to save data");
}else{
const char *dbpath = [_databasePath UTF8String];
sqlite3_stmt *statement = NULL;
if (sqlite3_open(dbpath, &_usersDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"UPDATE users SET companyname = \'%@\', companyaddress = \'%@\', companycity = \'%@\', companystate = \'%@\', companyzip = \'%@\', companyphone = \'%@\', companyfax = \'%@\', companyemail = \'%@\', companywebsite = \'%@\', driverfirstname = \'%@\', driverlastname = \'%@\', driveraddress = \'%@\', drivercity = \'%@\', driverstate = \'%@\', driverzip = \'%@\', driverphone = \'%@\', driverfax = \'%@\', driveremail = \'%@\', driverwebsite = \'%@\', WHERE user = \'%@\'",
self.companyName.text,
self.companyAddress.text,
self.companyCity.text,
self.companyState.text,
self.companyZip.text,
self.companyPhone.text,
self.companyFax.text,
self.companyEmail.text,
self.companyWebsite.text,
self.driverFirstName.text,
self.driverLastName.text,
self.driverAddress.text,
self.driverCity.text,
self.driverState.text,
self.driverZip.text,
self.driverPhone.text,
self.driverFax.text,
self.driverEmail.text,
self.driverWebsite.text,
[[PFUser currentUser] objectId]];
const char *query_stmt = [querySQL UTF8String];
sqlite3_finalize(statement);
sqlite3_prepare_v2(_usersDB, query_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Updated user in db");
} else {
NSLog(@"Failed to update user in db");
}
sqlite3_finalize(statement);
sqlite3_close(_usersDB);
}
}
有人能告诉我哪里出错了。我的代码继续使用#34;无法在db"中更新用户最后的NSLog声明。
提前致谢。
答案 0 :(得分:1)
您必须检查sqlite3_prepare_v2
的返回值。
如果sqlite3_prepare_v2
或sqlite3_step
失败,您应致电sqlite3_errmsg以获取有用的错误消息。
(并且该消息会告诉您在剩余逗号后WHERE无效。)