muoi tom
muoi tom

Reputation: 61

How to view the content of .db file?

I am using Xcode 6.4, developing apps for iOS 8 that need to use database. I inserted some information into my database ("something.db" file). I want to see the content of "something.db" to see what I inserted. How can I view its content?

This is my code for creating the database "company.db" and table "Staff"(ID PK, NAME TEXT, AGE INTEGER):

-(void) createOrOpenDB {
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [path objectAtIndex:0];
    dbPathString = [docPath stringByAppendingPathComponent:@"company.db"];
    char *error;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:dbPathString]) {
        const char *dbPath =[dbPathString UTF8String];
         if(sqlite3_open(dbPath, &personDB)==SQLITE_OK) {
            const char *sql_stmt = "CREATE TABLE IF NOT EXISTS Staff (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, AGE INTEGER)";
            sqlite3_exec(personDB, sql_stmt, NULL, NULL, &error);
            sqlite3_close(personDB);
        }
     }
}

This is my code for inserting information into the table "Staff":

- (IBAction)addPersonButton:(id)sender {
    char *error;
    if(sqlite3_open([dbPathString UTF8String], &personDB) == SQLITE_OK) {
        NSString *insertStmt = [NSString stringWithFormat:@"INSERT INTO Staff (NAME, AGE) values ('%s', '%d')", [self.nameField.text UTF8String], [self.ageField.text intValue]];
        const char *insert_stmt = [insertStmt UTF8String];
        if(sqlite3_exec(personDB, insert_stmt, NULL, NULL, &error) == SQLITE_OK) {
            NSLog(@"Person added");
            Person *person = [[Person alloc] init];
            [person setName:self.nameField.text];
            [person setAge:[self.ageField.text intValue]];
            [arrayofPerson addObject:person];
        }

        sqlite3_close(personDB);
    }
}

The code works well because I was able to display the information of the "Staff" table in the tableview. But how to view that information in the .db file?

p/s: I changed it to "something.xls" but Excel does not display human readable information. I also downloaded Fileviewpro but did not know how to use. Someone helps me please!

Thank you very much!

Upvotes: 2

Views: 7351

Answers (1)

Eli Waxman
Eli Waxman

Reputation: 2897

You can use any app that is designed to view the contents of an sqlite file. for example: http://sqlitebrowser.org

If you are running on simulator you can use ~/Library/Developer/CoreSimulator/Devices/1A8DF360-B0A6-4815-95F3-68A6AB0BCC78/data/Container/Data/Application/ to get to the simulator just replace the long UDID with your device.

Upvotes: 2

Related Questions