gpt4 book ai didi

iphone - 尝试在 SQLite (iOS) 中打开数据库时出错

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

我正在制作一个带有嵌入式 SQLite 数据库的 iOS 应用程序。因此,我在 SQLite 管理员中创建了我的数据库,并将其拖到我的 Xcode 项目中,就像教程中所说的那样。当我尝试打开我的数据库时,出现此错误:“内存不足”。不知道这是 SQLite 错误还是什么,但我的文件真的很小,导致内存问题。

这是我初始化数据库的代码:

- (id)initWithPath:(NSString *)path {
if (self = [super init]) {
BOOL success;
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"SoundLib_DB.sqlite"];

if ([fileManager fileExistsAtPath:dbPath] == NO) {
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"SoundLib_DB" ofType:@"sqlite"];
[fileManager copyItemAtPath:resourcePath toPath:dbPath error:&error];
}

success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
sqlite3 *dbConnection;
//Here is when I get the error, at trying to open the DB
if (sqlite3_open_v2("SoundLib", &dbConnection, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
NSLog(@"[SQLITE] Unable to open database!");
NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));
return nil;
}
database = dbConnection;
}
return self;

最佳答案

错误可能是因为您需要将数据库路径作为UTF8String 传递。我看到你正在通过“SoundLib”。你不能那样直接通过。

 if (sqlite3_open([dbPath UTF8String], &databaseHandle) == SQLITE_OK)
{
//dbPath is your full database path you getting above
}

附言也有 dbpath 作为 const char

const char *dbpath

关于iphone - 尝试在 SQLite (iOS) 中打开数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14484077/

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