Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create sqlite db programmatically in iphone sdk

hai i a'm trying to create a sqlite database programmatically at the run time. can anybody say how to create it in iphone sdk.

like image 839
KingofHeaven Avatar asked Aug 30 '10 10:08

KingofHeaven


2 Answers

Just call the sqlite3_open function it will create a database if no database exist on the path.

// generate databasePath programmatically
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
{

// your  code here
}

post a comment if you need more code example on this

like image 144
Saurabh Avatar answered Nov 15 '22 21:11

Saurabh


-(void)viewDidLoad
{
    [super viewDidLoad];
    NSString *docsDir;
    NSArray *dirPaths;
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]];
    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
            char *errMsg;
    const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";

            if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                NSLog(@"if");

            }

            sqlite3_close(contactDB);

        } else 
        {
            NSLog(@"else");

        }
    }
    [filemgr release];

}

-(IBAction)table
{
    NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.sqlite"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];

   // if ([filemgr fileExistsAtPath: databasePath ] == NO)
    {
        const char *dbpath = [databasePath UTF8String];

        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt = "CREATE TABLE LIST (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";

            if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {

                  NSLog(@"tables failed");
               // status.text = @"Failed to create table";
            }

            sqlite3_close(contactDB);

        }
        else 
        {

            NSLog(@"tables failed");
            //status.text = @"Failed to open/create database";

        }
    }

    [filemgr release];
}  
like image 25
Alok Chandra Avatar answered Nov 15 '22 19:11

Alok Chandra