如何使 FMDB 的数据库成为单例
我已经使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 SQLiteDB 类将需要维护对 FMDatabase 的引用,以便您的其他方法将能够共享同一数据库。
Your SQLiteDB class will need to maintain a reference to your FMDatabase so your additional methods will be able to share the same database.