반응형
기본형은
sqlite3 *database; NSString *databaseName = @"mbass4.db"; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; NSString *currentProgressString = @""; if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { NSString *query = @"select nm_prgs_phs \ from ddtbt_site;"; const char *sqlStatement = [query cStringUsingEncoding:NSASCIIStringEncoding]; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { // Loop through the results and add them to the feeds array while(sqlite3_step(compiledStatement) == SQLITE_ROW) { currentProgressString = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]; break; } } sqlite3_finalize(compiledStatement); } sqlite3_close(database); |
이게 완결이다. |
SqlUtils *db = [[SqlUtils alloc] init]; |
이렇게 쓰게 하기 위해서 |
- (id) init { if (self = [super init]) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; databasePath = [documentsDirectory stringByAppendingPathComponent:@"mbass4.db"]; } return } |
init을 만든다.- (id) init; |
- (void) selectSite:(DDTBT_SITE *)data { [self checkBackDB]; [data clear]; // Open the database from the users filessytem if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // Setup the SQL Statement and compile it for faster access NSString *query = @"select cd_site \ ,nm_site \ ,nm_site_abrv \ ,cd_prgs_phs \ ,nm_prgs_phs \ ,yn_adtn_phs \ ,cd_rcpt_phs \ ,nm_rcpt_phs \ from ddtbt_site;"; const char *sqlStatement = [query cStringUsingEncoding:NSASCIIStringEncoding]; sqlite3_stmt *compiledStatement; if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { // Loop through the results and add them to the feeds array while(sqlite3_step(compiledStatement) == SQLITE_ROW) { data.cd_site = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]; data.nm_site = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; data.nm_site_abrv = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; data.cd_prgs_phs = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; data.nm_prgs_phs = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)]; data.yn_adtn_phs = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)]; data.cd_rcpt_phs = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)]; data.nm_rcpt_phs = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)]; break; } } // Release the compiled statement from memory sqlite3_finalize(compiledStatement); } [self checkBackDB]; } |
기본형과 별 다른게 없다. 그냥 DB 가져오는 부분만 공통이니 뺏을 뿐. -(void) checkBackDB { if(database != nil) { if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { sqlite3_finalize; sqlite3_close(database); } else { sqlite3_finalize; sqlite3_close(database); } } sqlite3는 느리고 다른데서 쓰고 있을 때 계속 문제가 생겨서 finalize 와 close를 하나 만들었다. |
이제 기본형은
// // sql3hjh.h // // Created by Junho HA on 2021/05/26. // Copyright © 2021 hajunho.com All rights reserved. // #ifndef sql3hjh_h #define sql3hjh_h #import <sqlite3.h> @interface sql3hjh : NSObject { sqlite3 *database; } @property (nonatomic, strong, readwrite) NSString *databasePath; - (id) init; @end #endif /* sql3hjh_h */ |
// // sql3hjh.m // // Created by Junho HA on 2021/05/26. // Copyright © 2021 hajunho.com All rights reserved. // #import <Foundation/Foundation.h> #import "sql3hjh.h" @implementation sql3hjh @synthesize databasePath; - (id) init { if (self = [super init]) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; databasePath = [documentsDirectory stringByAppendingPathComponent:@"mbass4.db"]; } return self; } @end |
반응형
'C++' 카테고리의 다른 글
objc2C++select 구현 예제 (0) | 2021.05.26 |
---|---|
sqlite3 파일 생성 (0) | 2021.05.26 |
iOS Photokit (0) | 2021.05.26 |
ViewController 자료 교환 방식 수정 (0) | 2021.05.20 |
잘 되던게, 갑자기 안되는 문제 해결. (0) | 2021.05.20 |
최근댓글