如何使 FMDB 的数据库成为单例

发布于 2024-11-05 01:36:09 字数 2047 浏览 0 评论 0原文

我已经使用 SQLite 一段时间了,并决定转向 FMDB。我需要将其设为单例。下面是我的代码;我需要更改什么才能让 FMDB 访问单例 d/b?

#pragma mark Singleton Methods

+ (SQLiteDB *) sharedSQLiteDB  {

    if(!sharedSQLiteDB)  {
        sharedSQLiteDB = [[SQLiteDB alloc] init];
        [sharedSQLiteDB openCreateDB];  //  check to see if d/b exists
    }
    return sharedSQLiteDB;
}   

这是我使用 FMDB 初始化 d/b 的代码:

//-----------------------    checkIfDatabaseExists    -----------------|
    - (void) openCreateDB  {

        searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  // Get the path to the database file
        documentPath = [searchPaths objectAtIndex:0];
        databasePath = [documentPath stringByAppendingPathComponent:@"ppcipher.s3db"];
        cDatabasePath = [databasePath cStringUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"d/b path: /%@", databasePath);

        NSString *sqlCommand = @"CREATE TABLE CardData (card_id TEXT PRIMARY KEY NOT NULL, card_name TEXT NOT NULL, "
            @"card_type TEXT, code_val TEXT, create_date TEXT DEFAULT CURRENT_DATE, user_notes TEXT, gps_loc TEXT)"; 
        char * errmsg = nil;   

        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:databasePath error:NULL];  //  <------------  delete d/b  TESTING ONLY! 

        BOOL fileExists = [fileManager fileExistsAtPath:databasePath];
        if(!fileExists)  {
            FMDatabase* db = [FMDatabase databaseWithPath: databasePath]; 

            if (![db open]) {
                NSLog(@"Could not open/create database");
            }

            [db executeUpdate:@"CREATE TABLE CardData (card_id TEXT PRIMARY KEY NOT NULL, card_name TEXT NOT NULL, "
             @"card_type TEXT, code_val TEXT, create_date TEXT DEFAULT CURRENT_DATE, user_notes TEXT, gps_loc TEXT)"];

            if(errmsg != nil)
                NSLog(@"error: %s", errmsg);  //  DEBUGGING ONLY!  (REMOVE when done!)
        }
        return;
    }

I have been using SQLite for awhile now, and have decided to go to FMDB. I need to make it a singleton. Here's my code below; what do I have to change to have FMDB access the singleton d/b?

#pragma mark Singleton Methods

+ (SQLiteDB *) sharedSQLiteDB  {

    if(!sharedSQLiteDB)  {
        sharedSQLiteDB = [[SQLiteDB alloc] init];
        [sharedSQLiteDB openCreateDB];  //  check to see if d/b exists
    }
    return sharedSQLiteDB;
}   

and this is the code I use to initialize the d/b using FMDB:

//-----------------------    checkIfDatabaseExists    -----------------|
    - (void) openCreateDB  {

        searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  // Get the path to the database file
        documentPath = [searchPaths objectAtIndex:0];
        databasePath = [documentPath stringByAppendingPathComponent:@"ppcipher.s3db"];
        cDatabasePath = [databasePath cStringUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"d/b path: /%@", databasePath);

        NSString *sqlCommand = @"CREATE TABLE CardData (card_id TEXT PRIMARY KEY NOT NULL, card_name TEXT NOT NULL, "
            @"card_type TEXT, code_val TEXT, create_date TEXT DEFAULT CURRENT_DATE, user_notes TEXT, gps_loc TEXT)"; 
        char * errmsg = nil;   

        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:databasePath error:NULL];  //  <------------  delete d/b  TESTING ONLY! 

        BOOL fileExists = [fileManager fileExistsAtPath:databasePath];
        if(!fileExists)  {
            FMDatabase* db = [FMDatabase databaseWithPath: databasePath]; 

            if (![db open]) {
                NSLog(@"Could not open/create database");
            }

            [db executeUpdate:@"CREATE TABLE CardData (card_id TEXT PRIMARY KEY NOT NULL, card_name TEXT NOT NULL, "
             @"card_type TEXT, code_val TEXT, create_date TEXT DEFAULT CURRENT_DATE, user_notes TEXT, gps_loc TEXT)"];

            if(errmsg != nil)
                NSLog(@"error: %s", errmsg);  //  DEBUGGING ONLY!  (REMOVE when done!)
        }
        return;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2024-11-12 01:36:09

您的 SQLiteDB 类将需要维护对 FMDatabase 的引用,以便您的其他方法将能够共享同一数据库。

@interface SQLiteDB : NSObject //Or whatever base class
{
    FMDatabase *_database;
}

@end

//implementation

- (void) openCreateDB  {
   ...
   if(!fileExists)  {
      _database = [[FMDatabase databaseWithPath: databasePath] retain];
   ...
   }
}

Your SQLiteDB class will need to maintain a reference to your FMDatabase so your additional methods will be able to share the same database.

@interface SQLiteDB : NSObject //Or whatever base class
{
    FMDatabase *_database;
}

@end

//implementation

- (void) openCreateDB  {
   ...
   if(!fileExists)  {
      _database = [[FMDatabase databaseWithPath: databasePath] retain];
   ...
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文