我的应用中存在问题而且我不知道它来自哪里......
所以我将我的文件添加到我的项目突出显示!
但是当我想在我的表中初始化并选择时,我有一个错误:
no such table
我很确定桌子在这里因为在我的终端我得到了这个:
我在代码中执行相同的请求:
tableData = [sqlManager getList:@"SELECT name_fr FROM info_max"];
sqlManager与其他3个文件一起使用..
我用它来初始化
-(id)initDatabase:(NSString*)dbName {
if (self = [super init]) {
//Nom de la base de données
databaseName = dbName;
//Obtention du chemin complet de la base de données
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
}
return self;
}
修改
我的查询执行方式如下:
-(NSMutableDictionary *)getList:(NSString*)typeOfRequest{
sqlite3 *database;
const char *sqlStatement = [typeOfRequest UTF8String];
NSMutableDictionary *aromaArray = [[NSMutableDictionary alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
sqlite3_stmt *compiledStatement;
//Compilation de la requete et verification du succes
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSDictionary *dictionary = [self indexByColumnName:compiledStatement]; // Creation d'un dictionnaire des noms de colonnes
NSMutableArray *array = [[NSMutableArray alloc] init];
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
char *tab = (char*)sqlite3_column_text(compiledStatement, [[dictionary objectForKey:nil] intValue]);
NSString *finalUpperCase = [[NSString stringWithUTF8String:tab] capitalizedString];
if (finalUpperCase != nil && finalUpperCase.length >0 && !([finalUpperCase isEqualToString:@"Null"])) {
[array addObject:finalUpperCase];
}
}
aromaArray = [self getDictionnary:array];
}
else {
NSAssert1(0, @"Erreur :. '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement); // Finalise la requete et libere le statement
}
else {
NSAssert(0, @"Erreur d'ouverture de la base de donnees");
}
sqlite3_close(database);
return aromaArray;
}
答案 0 :(得分:0)
有时Xcode乱七八糟,它的派生数据会保留文件的引用,因此有时您需要清除产品并再次运行它。
对于不够的插入和更新查询:
将此文件复制到bundle(目标)时,您只能将此文件设为只读。为了使其可读并且能够进行插入查询,请遵循以下解决方案:
此代码在应用程序Documents目录中创建捆绑的默认数据库的可写副本。将其放在application didFinishLaunchingWithOptions
appDelegate
上,现在您就可以测试您的查询了:
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"info_max.sqlite3"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"info_max.sqlite3"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
答案 1 :(得分:0)
您还必须尝试将Database(sqlite)文件移至项目目录之外,然后再次添加它并清除所有缓存数据。
这对我有用。