關於iOS中SQLITE控制代碼的使用的細節

來源:互聯網
上載者:User

標籤:

1、設計思想:給SQLITE進行封裝,利用定義的類別實現方法的簡潔,以便達到低耦合效果

 1 控制器代碼: 2 #import "ViewController.h" 3 #import "DatabaseHelper.h" 4  5 @interface ViewController () 6 { 7      sqlite3 *db; 8 } 9 @end10 11 @implementation ViewController12 13 -(void)loadView {14 15     [super loadView];16     self.view.backgroundColor = [UIColor orangeColor];17 }18 - (void)viewDidLoad {19     [super viewDidLoad];20     //PART1:該部分為實現的協助類別21     //PART1 .122   [DatabaseHelper openDatabaseWithName:@"personinfo.sqlite" withSqlite:db];23     //PART1 .224    db =[DatabaseHelper openDatabaseWithName:@"personinfo.sqlite" withSqlite:db]; 
25 NSLog(@" isOpenOrCreat = %@",db ?@"sucess":@"failure");
26
27 //PART2:用以判斷資料庫是否開啟
28 if (db) {
29
30 }
31
32 //PART3:用以判斷資料庫是否開啟
33 [DatabaseHelper execSql:@"CREATE TABLE IF NOT EXISTS PERSONINFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, address TEXT)" withSqlite:db];

 

 1 操作類代碼: 2 #import "DatabaseHelper.h" 3  4 @implementation DatabaseHelper 5 +(sqlite3 *)openDatabaseWithName:(NSString *)name  withSqlite:(sqlite3 *)sqlite3{ 6     //PART5: 7     //1、擷取Documents檔案路徑 8     NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]; 9     //2、拼接要建立的資料庫檔案路徑10     NSString *databasePath = [documentsPath stringByAppendingPathComponent:name];11     //列印12     NSLog(@"databasePath = %@",databasePath);13     //判斷是否已經開啟(開啟失敗時關閉資料庫並log提示)14     if (sqlite3_open([databasePath UTF8String], &sqlite3) != SQLITE_OK) {15         sqlite3_close(sqlite3);16         NSLog(@"資料庫開啟失敗");17         return nil ;18     }19 20 //PART4:21 //    char *err;22 //    NSString *sql = @"CREATE TABLE IF NOT EXISTS PERSONINFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, address TEXT)";23 //    if (sqlite3_exec(sqlite3, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {24 //        sqlite3_close(sqlite3);25 //        NSLog(@"資料庫操作資料失敗!");26 //        NSLog(@"%s ",err);27 //    }28 29     return  sqlite3;30 }31 32 +(BOOL)execSql:(NSString *)sql  withSqlite:(sqlite3 *)sqlite3{33     char *err;34     //sqlite3_exec(),   執行非查詢的sql語句35     if (sqlite3_exec(sqlite3, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {36         sqlite3_close(sqlite3);37         NSLog(@"資料庫操作資料失敗!");38         return NO;39     }40     return YES;41 42 }

 

問題:1、當注掉PART1.1                          資料庫可以被寫入2、當注掉PART1.1、PART4              後資料庫不能被寫入 3、當注掉PART1.2、PART4              資料庫可以被寫入結論:1、控制代碼的存在必須依賴於上下文,資料庫的操作必須依賴於控制代碼存在的內容。2、只有控制代碼前後相對應才能實現對資料庫的操作.   

關於iOS中SQLITE控制代碼的使用的細節

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.