-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 ?

  • 2
    possible duplicate of [How to create sqlite database programmatically?](http://stackoverflow.com/questions/10545520/how-to-create-sqlite-database-programmatically) – Fonix Sep 08 '15 at 09: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. – Rohit Kumar Sep 08 '15 at 09: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: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html – Neil Billingham Sep 08 '15 at 09:52

3 Answers3

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));
    }

}
Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
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);
    }

}
user3182143
  • 9,459
  • 3
  • 32
  • 39
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.
}
lynulzy
  • 561
  • 7
  • 19