create sqlite db programmatically in iphone sdk

后端 未结 3 1864
轮回少年
轮回少年 2021-01-02 14:33

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.

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-02 15:36

    -(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];
    }  
    

提交回复
热议问题