dimanche 5 avril 2015

sqlite3_prepare_v2 error no such table

This is my Sql helper :



- (void)loadDb
{
[self createDocumentsDirIfNeeded];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingFormat:@"/C.A.S.E..sqlite"]; // C.A.S.E. is the name
//NSLog(@"The path is %@",path);

BOOL success = [fileManager fileExistsAtPath:path]; // No

// If the writable database does not exist, so copy the default to the appropriate location
if (!success)
{
//NSLog(@"File not found");
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"C.A.S.E..sqlite"];
NSLog(@"The default path is %@",defaultDBPath);
success = [fileManager copyItemAtPath:defaultDBPath toPath:path error:nil];
if (!success) // No
{
NSLog(@"Data not copied");
}
}

// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &database) != SQLITE_OK) { // Ok
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(database);
NSLog(@"Failed to open database with message '%s'.", sqlite3_errmsg(database));
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
// Additional error handling, as appropriate...
}
}

- (void)createDocumentsDirIfNeeded {
// Workaround for Beta issue where Documents directory is not created during install.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

BOOL exists = [fileManager fileExistsAtPath:documentsDirectory]; // Yes
if (!exists) {
BOOL success = [fileManager createDirectoryAtPath:documentsDirectory attributes:nil];
if (!success) {
NSAssert(0, @"Failed to create Documents directory.");
}
}
}


I have written the boolean values (Yes/ No) I am getting while debugging. As a result of this empty database is created with no tables.


This is where I am getting records :



- (NSMutableArray*)GetAllProjects{

NSMutableArray *arrProjects = [[NSMutableArray alloc]init];

sqlite3_stmt *statement = nil;

@try {
const char *sql = "SELECT * FROM Projects";

if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
NSLog(@"success");
while (sqlite3_step(statement) == SQLITE_ROW) {

char *dbString;

Project *project = [[Project alloc]init];

dbString = (char *)sqlite3_column_text(statement, 0);
NSString *ProjectId = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
[project setProjectId:ProjectId];

dbString = (char *)sqlite3_column_text(statement, 1);
NSString *ProjectName = (dbString) ? [NSString stringWithUTF8String:dbString] : @"";
[project setName:ProjectName];

[arrProjects addObject:project];
}
}
else
{
NSLog(@"Prepare-error #%i: %s", sqlite3_prepare_v2(database, sql, -1, &statement, NULL), sqlite3_errmsg(database));
}



}
@catch (NSException *exception) {

@throw exception;
}
@finally {

sqlite3_finalize(statement);
}

return arrProjects;
}


else block is called.


Aucun commentaire:

Enregistrer un commentaire