For some reason, my app doesn't seem to create a database in the Documents folder. I have been following this video tutorial (http://www.youtube.com/watch?v=bC3F8a4F_KE) (it also has the source code available (https://github.com/iffytheperfect1983/sqliteTutorial) which I downloaded and it ran on the Simulator perfectly).
I used a few NSLog statements to attempt to pinpoint the problem. I realized that while all of the code up to "Declared" statement ran with no problems, the rest of the NSLog statements didn't show up in the console. I have looked tirelessly back and forth between the code I wrote and the code in the tutorial but with no avail for a solution.
Here is the code.
@interface ViewController () {
NSMutableArray *letterArray;
sqlite3 *letterDB;
NSString *dbPathString;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Set up
letterArray = [[NSMutableArray alloc]init];
[[self letterList] setDelegate:self];
[[self letterList] setDataSource:self];
// create or open database
[self createOpenDB];
// Display database
[self displayDB];
}
// Create/open database
- (void) createOpenDB {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:@"letters.db"];
char *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
const char *dbPath = [dbPathString UTF8String];
NSLog(@"Declared");
// create database here
if (sqlite3_open(dbPath, &lettersDB)==SQLITE_OK) {
NSLog(@"'If' statement is ok");
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS LETTERS (ID INTEGER PRIMARY KEY AUTOINCREMENT, A TEXT, B TEXT, C TEXT, D TEXT, E TEXT, F TEXT, G TEXT)";
NSLog(@"Declared");
sqlite3_exec(lettersDB, sql_stmt, NULL, NULL, &error);
NSLog(@"Execution complete.");
sqlite3_close(lettersDB);
NSLog(@"Close complete.");
NSLog(@"Database creation successful.");
}
}
}
How do I solve this? Thank you in advance.
You're using the NSDocumentationDirectory
when you should be using the NSDocumentDirectory
directory.
Change this:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
To this:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);