mercredi 2 septembre 2015

read from the same database without keep creating new database in a new a new temporary folder

i have an iOS app database sqlite but i notice when i close the Xcode and run again and run simulator the app create a new database and store it in a new temporary folder each time in something like this bath

//Users/username/Library/Developer/CoreSimulator/Devices/798751E5-BF62-4A61-BA8A-425FF85C4A55/data/Containers/Data/Application/A78565B8-A887-4942-9962-AA5CAD19E13C/Documents/MLGBoxDB.db

so how i can make my app keep the database in one place and read from the same database without keep creating new database in a new a new temporary folder each time

i use this code to create Database

NSString *DocsDir;
NSArray * DirPath;

// getting the documents directory
DirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,   NSUserDomainMask, YES);
DocsDir = DirPath[0];

DataBasePath = [[NSString alloc] initWithString:[DocsDir stringByAppendingPathComponent:@"MLGBoxDB.db"]];
NSLog(@"%@",self.DataBasePath);
NSFileManager *FileMangr = [NSFileManager defaultManager];

    if ([FileMangr fileExistsAtPath:DataBasePath]==YES)
{
    const char *dbPath =[DataBasePath UTF8String];
    if (sqlite3_open(dbPath, &MLGBoxDB)==SQLITE_OK)
    {
        char * errorMsg;
        //CREATE TABLE NAMED MLGBOXGAME
        const char *Sql_Stetment = "CREATE TABLE IF NOT EXISTS MLGBOXGAME(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT)";
        if (sqlite3_exec(MLGBoxDB, Sql_Stetment, NULL, NULL, &errorMsg)!= SQLITE_OK)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to create table"
                                                            message:@"Failed to create Player Name table"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];

        } sqlite3_close(MLGBoxDB);
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to open database"
                                                    message:@"Failed to open database"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

}

Aucun commentaire:

Enregistrer un commentaire