所以我想要做的是当我按下按钮时对我的sqlite数据库运行UPDATE查询。下面是我的,它不起作用,我认为问题是我从捆绑打开数据库,这当然是只读的。现在我一直在做一些研究,我意识到我应该:
检查Documents文件夹中是否存在数据库。
如果没有,请将数据库从捆绑包复制到Documents文件夹。
现在,从Documents文件夹中打开数据库。
但我不知道怎么办???任何帮助将不胜感激。这就是我所拥有的:
- (IBAction)update:(id)sender {
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "UPDATE strengthexercises SET isFavorite = 'true' WHERE ID = '1'";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}else{
if (sqlite3_step(sqlStatement) == SQLITE_DONE){
NSLog(@"Success");
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
@finally {
sqlite3_close(db);
}
}
有一个例子的明确答案会有很大帮助。提前谢谢。
更新#1
现在我有了这个,但它仍然不起作用......
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
- (IBAction)update:(id)sender {
@try {
NSString *docsPath = [self applicationDocumentsDirectory];
NSString *dbPath = [docsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];
if (!fileExists) {
// get the source path to copy from
NSString *dbSourcePath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
// copy db to documents
[[NSFileManager defaultManager] copyItemAtPath:dbSourcePath toPath:dbPath error:nil];
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "UPDATE strengthexercises SET isFavorite = 'true' WHERE ID = '1'";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}else{
if (sqlite3_step(sqlStatement) == SQLITE_DONE){
NSLog(@"Success");
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
@finally {
sqlite3_close(db);
}
}
答案 0 :(得分:0)
编写一个方法来返回这样的文档路径 -
- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
然后检查文档路径中是否存在db -
NSString *docsPath = [self applicationDocumentsDirectory];
NSString *dbDocsPath = [docsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbDocsPath];
使用条件来确定db是否需要复制,如果是,则复制 -
if (!fileExists) {
// get the source path to copy from
NSString *dbSourcePath = [[NSBundle mainBundle] pathForResource:@"StayhealthyExercises-1" ofType:@"sqlite"];
// copy db to documents
[[NSFileManager defaultManager] copyItemAtPath:dbSourcePath toPath:dbDocsPath error:nil];
}
现在,您应该在可以读/写的文档中拥有数据库的副本。