EGODatabase

[objective-c]シングルトンクラス

シングルトンクラス

DataBaseManager.h

#import <Foundation/Foundation.h>
#import "EGODatabase.h"

@interface DataBaseManager : NSObject{
}
+ (DataBaseManager*)sharedInstance;
@end

DataBaseManager.m

#import "DataBaseManager.h"

@implementation DataBaseManager

+ (DataBaseManager*)sharedInstance {
    static DataBaseManager* _instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[DataBaseManager alloc]initSharedInstance];
    });
    return _instance;
}

- (id)initSharedInstance {
    self = [super init];
    if (self) {
    	//初期化
    }
    return self;
}
@end

使うとき

DataBaseManager*dbm=[DataBaseManager sharedInstance];

[objective-c]EGODatabase #9 カラムの型一覧取得

カラムの型一覧取得

-(EGODatabase*)getDB
{
    EGODatabase* database = [EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]];
    return database;
}
-(NSDictionary*)getColumnTypesByTableName:(NSString*)tableName
{
    EGODatabase*db=[self getDB];
    NSString*sql=[NSString stringWithFormat:@"pragma table_info(%@)",tableName];
    EGODatabaseResult*result=[db executeQuery:sql];
    NSMutableDictionary*dic=[NSMutableDictionary dictionary];
    for (EGODatabaseRow*row in result) {
        [dic setObject:[row stringForColumn:@"type"] forKey:[row stringForColumn:@"name"]];
    }
    return dic;
}

[objective-c]EGODatabase #8 カラム名一覧取得

カラム名一覧取得

-(NSArray*)getColumnNamesByTableName:(NSString*)tableName{
    EGODatabase*db=[self getDB];
    NSString*sql=[NSString stringWithFormat:@"pragma table_info(%@)",tableName];
    EGODatabaseResult*result=[db executeQuery:sql];
    NSMutableArray*list=[NSMutableArray arrayWithCapacity:[result count]];
    for (EGODatabaseRow*row in result) {
        [list addObject:[row stringForColumn:@"name"]];
    }
    return [NSArray arrayWithArray:list];
}

[objective-c]EGODatabase #7 テーブル一覧取得

テーブル一覧取得

-(NSArray*)getTableNameList{
    EGODatabase*db=[self getDB];
    NSString*sql=@"select name from sqlite_master where type = 'table' order by name";
    EGODatabaseResult*result=[db executeQuery:sql];
    NSMutableArray*list=[NSMutableArray arrayWithCapacity:[result count]];
    for (EGODatabaseRow*row in result) {
        [list addObject:[row stringForColumn:@"name"]];
    }
    return [NSArray arrayWithArray:list];
}
-(EGODatabase*)getDB{
    EGODatabase* database = [EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]];
    return database;
}
-(BOOL)isExistTableByName:(NSString*)tableName{
    NSArray*tables=[self getTableNameList];
    return [tables containsObject:tableName];
}

[objective-c]EGODatabase #6

EGODatabase #6
下記の処理を実行しただけではuser.dbは作成されていない。
ファイルはexecuteQueryを実行すると作成される。

NSString*filepath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/user.db"];
EGODatabase* database = [EGODatabase databaseWithPath:filepath];

[objective-c]EGODatabase #5 データ追加

EGODatabase*db=[EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]];
NSString*sql=[NSString stringWithFormat:@"insert into %@ (id,filename) values(%d,%@)",tableName,id,fileName];
EGODatabaseResult*result=[db executeQuery:sql];
if (result.errorCode) {//failed
    return NO;
}
return YES;

参考サイト
enormego/egodatabase · GitHub
https://github.com/enormego/egodatabase

[objective-c]EGODatabase #4 データ取得

EGODatabase*db=[EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]];
NSString*sql=@"SELECT * FROM tableName WHERE no=1 ORDER BY id;"
EGODatabaseResult*result=[db executeQuery:sql];
if (result.errorCode) {//failed
    return;
}
NSMutableArray*list=[NSMutableArray arrayWithCapacity:[result count]];
for (EGODatabaseRow*row in result) {
    [list addObject:[row stringForColumn:@"name"]];
}

参考サイト
enormego/egodatabase · GitHub
https://github.com/enormego/egodatabase

[objective-c]EGODatabase #3 全テーブル名の取得

EGODatabase*db=[EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]];
NSString*sql=@"select name from sqlite_master where type = 'table' order by name";
EGODatabaseResult*result=[db executeQuery:sql];
if (result.errorCode) {//failed
    return;
}
NSMutableArray*list=[NSMutableArray arrayWithCapacity:[result count]];
for (EGODatabaseRow*row in result) {
    [list addObject:[row stringForColumn:@"name"]];
}

参考サイト
enormego/egodatabase · GitHub
https://github.com/enormego/egodatabase

[objective-c]EGODatabase #1 テーブル作成

EGODatabaseはスレッドセーフなSQLiteのラッパーです。

-(EGODatabase*)getDB{
    EGODatabase* database = [EGODatabase databaseWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/database.db"]];
    return database;
}
EGODatabase* database=[self getDB];
NSString*sql=[NSString stringWithFormat:@"create table if not exists '%@' (id int,gno int,type int,filename varchar);",tableName];
EGODatabaseResult*result= [database executeQuery:sql];
if (result.errorCode) {//failed
        
}else{//success
        
}

参考サイト
enormego/egodatabase · GitHub
https://github.com/enormego/egodatabase