我想在我的iOS應用程序中使用Sqlite3創建數據庫。如果我在文檔目錄的子文件夾中創建數據庫,它不會創建數據庫,它無法打開或創建數據庫。如果我不在子文件夾中創建數據庫,而是直接在文檔目錄中創建數據庫。它正在創造它。無法在文檔目錄的子文件夾中創建sqlite數據庫
這是給路徑databasePath: 「在/ var /移動/集裝箱/數據/應用/ 986037DB-6FA0-4066-9977-C5D7A075C5E7 /做cuments/MailFolder/INBOX.db」 但它在失敗 - >if (sqlite3_open(dbpath, &database) == SQLITE_OK)
- 下面是我在一個子文件夾中創建數據庫的代碼。有人能糾正我這裏有什麼問題嗎?
- (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;
}
您需要先創建子文件夾。 – rmaddy
順便說一句 - 擺脫「stringByAppendingPathComponent」調用中的「斜線」。該方法的重點在於確保爲您添加正確的路徑分隔符。 – rmaddy
我只是首先創建子文件夾。它是給路徑爲「/var/mobile/Containers/Data/Application/986037DB-6FA0-4066-9977-C5D7A075C5E7/Documents/MailFolder/INBOX.db」但這裏是失敗 - > if(sqlite3_open(dbpath,&database) == SQLITE_OK) – user1953977