-1

I am fresher in iOS. I want to create database in my application. I am little bit confused regarding How I can able to create it ?

3
  • 2
    possible duplicate of How to create sqlite database programmatically? Commented Sep 8, 2015 at 9:25
  • I would recommend to create your database structure using some tools like Plugins in Firefox and then copy your database file in document directory. Commented Sep 8, 2015 at 9:38
  • Core Data is Apples framework in this area. It's not a database but is used for managing objects. It can use SQLite as one of its persistent store types.This might be a good place to start: developer.apple.com/library/ios/documentation/Cocoa/Conceptual/… Commented Sep 8, 2015 at 9:52

3 Answers 3

0
- (void)CreateDatabase
{
    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [paths objectAtIndex:0];
    NSString *finalPath = [documentPath stringByAppendingPathComponent:@"CabManagement.sqlite"];

    success = [fileManager fileExistsAtPath:finalPath];

    if(success)
    {
        NSLog(@"Database Already Created.");
        return;
    }

    NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CabManagement.sqlite"];

    success = [fileManager copyItemAtPath:defaultPath toPath:finalPath error:&error];

    if(success)
    {
        NSLog(@"Database Created Successfully.");
    }
}

- (void)InitializeDatabase
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [paths objectAtIndex:0];
    NSString *finalPath = [documentPath stringByAppendingPathComponent:@"CabManagement.sqlite"];

    if(sqlite3_open([finalPath UTF8String], &dbCabManagement) != SQLITE_OK)
    {
        sqlite3_close(dbCabManagement);
        NSLog(@"Error to Open Database :- %s",sqlite3_errmsg(dbCabManagement));
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Creating database programmatically

NSArray *dirPath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *docDir = dirPath[0];

NSString *databasePath = [[NSString alloc]initWithString:[docDir stringByAppendingPathComponent:@"YourDBName.db"]];

NSLog(@"the DB path:%@",databasePath);

NSFileManager *fileMgr =[NSFileManager defaultManager];

if ([fileMgr fileExistsAtPath:databasePath] == NO)

{
    const char *dbPath = [databasePath UTF8String];

    if (sqlite3_open(dbPath, &mydatabase) == SQLITE_OK)

    {
        char *errMsg;

        const char *sql_stmt ="CREATE TABLE IF NOT EXISTS YourTableName (ID INTEGER PRIMARY KEY AUTOINCREMENT,Name Text,Age Text,Image Text,Location Text,Date Text,Time Text,MilliSecondsTime Text,Address Text)";

        NSString *Status;            

        if (sqlite3_exec(mydatabase, sql_stmt, NULL, NULL, &errMsg))

        {

            Status =@"Failed to Create Table";
        }

        else

        {
          Status =@"Suucess in Create Table";
        }

        NSLog(@"%@",Status);

        sqlite3_close(mydatabase);
    }

    else

    {

        Status =@"Failed to Open/Create Database";

        NSLog(@"%@",Status);
    }

}

Comments

0

Generally , I would like to use FMDB by ccgus to manage my database and there is no need for me to care about How to save the database.

 NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"db"];
    FMDatabase *demoDB = [[FMDatabase alloc] initWithPath:path];
    if(demoDB.open == NO) {
    //Something is wrong when open the "demo.db" database
} else {
    //Open the database success.Then you kan run some sql query in the database.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.