如何在SqliteDatabase中创建数据库

时间:2015-09-08 09:20:56

标签: ios sqlite

我在iOS中比较新鲜。我想在我的应用程序中创建数据库。我对如何创建它有点困惑?

3 个答案:

答案 0 :(得分: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));
    }

}

答案 1 :(得分:0)

以编程方式创建数据库

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

}

答案 2 :(得分:0)

通常,我想使用FMDB ccgus来管理我的数据库,我不需要关心如何保存数据库。

 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.
}