Iphone 尝试在SQLite(iOS)中打开数据库时出错
我正在制作一个带有嵌入式SQLite数据库的iOS应用程序。所以我在SQLite管理员中创建了我的DB,并将它拖到我的Xcode项目中,就像教程中所说的那样。 当我试图打开我的数据库时,我得到了这个错误:内存不足。我不知道这是SQLite错误还是别的什么,但我的文件太小,导致内存问题 这是我初始化数据库的代码:Iphone 尝试在SQLite(iOS)中打开数据库时出错,iphone,ios,objective-c,sqlite,Iphone,Ios,Objective C,Sqlite,我正在制作一个带有嵌入式SQLite数据库的iOS应用程序。所以我在SQLite管理员中创建了我的DB,并将它拖到我的Xcode项目中,就像教程中所说的那样。 当我试图打开我的数据库时,我得到了这个错误:内存不足。我不知道这是SQLite错误还是别的什么,但我的文件太小,导致内存问题 这是我初始化数据库的代码: - (id)initWithPath:(NSString *)path { if (self = [super init]) { BOOL success; NSErro
- (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
}
p.S.也将dbpath作为常量char
更改打开过程,使其看起来像这样。。。它对我有用; 我猜你的主要错误是忘记了UTF8字符串
sqlite3 *database;
int result = sqlite3_open([dbPath UTF8String], &database);
if (result != SQLITE_OK) {
sqlite3_close(database);
NSLog(@"Error opening databse");
return;
}
在sqlite3_open_v2调用中,您没有使用数据库的实际文件路径。是的,sqlite3_open_v2SoundLib,&dbConnection,。。。这不仅仅是可疑的,它是错误的,我把上一个标记为正确的,只是因为我先读了它,但这也适用于男人。谢谢,随时都可以。忠告:一定要保持阅读苹果官方文档的习惯。仅仅依靠教程将建立你的编码技能的薄弱基础。
sqlite3 *database;
int result = sqlite3_open([dbPath UTF8String], &database);
if (result != SQLITE_OK) {
sqlite3_close(database);
NSLog(@"Error opening databse");
return;
}