我正在尝试在我的iOS应用程序中使用Sqlite3创建数据库。如果我在文档目录的子文件夹中创建数据库,它不是创建数据库,它无法打开或创建数据库。如果我不在子文件夹中创建数据库,而是直接在文档目录中创建它。它正在创造它。
它将路径作为databasePath:" / var / mobile / Containers / Data / Application / 986037DB-6FA0-4066-9977-C5D7A075C5E7 / Do cuments / MailFolder / INBOX.db"
但它失败了 - > if (sqlite3_open(dbpath, &database) == SQLITE_OK)
-
下面是我在子文件夹中创建DB的代码。有人可以纠正我这里有什么问题吗?
- (BOOL) createFolderMailDB :(NSString *) dbName {
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
//docsDir =[[dirPaths objectAtIndex:0] stringByAppendingPathComponent:@"MailFolder"];
NSString *documentsPath = [dirPaths objectAtIndex:0];
docsDir = [documentsPath stringByAppendingPathComponent:@"/MailFolder"];
//docsDir = dirPaths[0];
// Build the path to the database file
NSMutableString *finalDBPath = [[NSMutableString alloc]init];
[finalDBPath appendString:dbName];
[finalDBPath appendString:@".db"];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: finalDBPath]];
NSLog(@"databasePath: %@", databasePath);
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "create table if not exists MailFolderDBTable (emailid text, foldername text, messagedata text)";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(@"Failed to create table");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(@"Failed to open/create database");
}
}
return isSuccess;
}
答案 0 :(得分:3)
您放置此数据库的文件夹必须存在,否则尝试创建数据库将失败。因此,在继续创建数据库之前创建该文件夹:
windows()