gpt4 book ai didi

ios - 为什么 NSBundle mainBundle 有效,但 documentsDirectory 无效?

转载 作者:行者123 更新时间:2023-11-28 19:18:59 25 4
gpt4 key购买 nike

我正在尝试建立与数据库的连接,我发现当我将路径设置为 NSBundle 时成功,但当我尝试将路径设置在我的应用程序的文档目录中时却没有成功。这是我的代码:

-(IBAction)setInput:(id)sender
{
NSString *strStoreNumber;
NSString *strRegNumber;

strStoreNumber = StoreNumber.text;
strRegNumber = RegNumber.text;

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths lastObject];
NSString* databasePath = [documentsDirectory stringByAppendingPathComponent:@"tblStore.sqlite"];
// NSString* databasePath = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"Opened sqlite database at %@", databasePath);
sqlite3_exec(database, "CREATE TABLE IF NOT EXISTS tblStore (ID INTEGER PRIMARY KEY AUTOINCREMENT, Message TEXT)", NULL, NULL, NULL);
//...stuff
}
else
{
NSLog(@"Failed to open database at %@ with error %s", databasePath, sqlite3_errmsg(database));
sqlite3_close (database);
}
//
NSString *querystring;

// create your statement
querystring = [NSString stringWithFormat:@"SELECT strStore, strReg FROM tblStore WHERE strStore = %@ AND strReg = %@;", strStoreNumber, strRegNumber];

const char *sql = [querystring UTF8String];

NSString *szStore = nil;
NSString *szReg = nil;

sqlite3_stmt *statement = nil;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL)!=SQLITE_OK) //queryString = Statement
{
NSLog(@"sql problem occured with: %s", sql);
NSLog(@"%s", sqlite3_errmsg(database));
}
else
{
// you could handle multiple rows here
while (sqlite3_step(statement) == SQLITE_ROW)
{
szStore = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
szReg = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
}

}

sqlite3_finalize(statement);

lblStoreNumber.text = szStore;
lblRegNumber.text = szReg;
//
}

我注释掉了这行:

NSString* databasePath = [[NSBundle mainBundle] pathForResource:@"tblStore" ofType:@"sqlite"];

当这一行没有被注释掉,而它上面的行被注释掉时:

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString* documentsDirectory = [paths lastObject];

NSString* databasePath = [documentsDirectory stringByAppendingPathComponent:@"tblStore.sqlite"];

然后它工作正常。但是,如果这三行没有被注释掉(如 setInput 方法中所示,则会出现以下错误:

2012-05-07 13:44:29.511 CCoDBTry[1981:f803] Opened sqlite database at    /Users/Matt****/Library/Application Support/iPhone Simulator/5.1/Applications/5DB7A218-A0F6-  485F-B366-91FD2F9BC062/Documents/tblStore.sqlite
2012-05-07 13:44:29.545 CCoDBTry[1981:f803] sql problem occured with: SELECT strStore, strReg FROM tblStore WHERE strStore = 8053 AND strReg = 4;
2012-05-07 13:44:29.546 CCoDBTry[1981:f803] no such column: strStore

请记住,当我使用 NSBundle 逻辑时,访问同一个数据库表并且工作正常。我承认我不完全理解 NSBundle 和 documentsDirectory 之间的区别,但我想我希望我的表存在于我的应用程序的文档中。对此,我将不胜感激。

谢谢!

最佳答案

NSBundle 用于访问应用程序本身内的资源:即 YourApp.app 内的所有内容。 documentsDirectory 是您应用之外的一个位置——它位于“主目录”中,它是您应用沙箱的一部分,类似于 Mac 上的用户主目录。

这些是不同的位置,因此使用一个在另一个的相同子路径中查找文件是行不通的。

关于ios - 为什么 NSBundle mainBundle 有效,但 documentsDirectory 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com