iOS學習筆記(十五)——資料庫操作(SQLite)

來源:互聯網
上載者:User

 http://www.sqlite.org/docs.html

 

 

-(BOOL) openDB{   //擷取資料庫路徑    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documents = [paths objectAtIndex:0];    NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];        //如果資料庫存在,則用sqlite3_open直接開啟(不要擔心,如果資料庫不存在sqlite3_open會自動建立)    //開啟資料庫,這裡的[path UTF8String]是將NSString轉換為C字串,因為SQLite3是採用可移植的C(而不是    //Objective-C)編寫的,它不知道什麼是NSString.    if (sqlite3_open([database_path UTF8String], &db) == SQLITE_OK) {        return YES;    }else{        return NO;        NSLog(@"資料庫開啟失敗");        sqlite3_close(db);    }}

 

 

-(void)execSql:(NSString *)sql{        if ([self openDB]) {        char *err;        if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {            NSLog(@"資料庫操作資料失敗!");        }else{            NSLog(@"%@",sql);        }        sqlite3_close(db);    }    }

 

 

-(void) insertData{    NSString *insertSql1= [NSString stringWithFormat:                      @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",                      TABLENAME, NAME, AGE, ADDRESS, @"張三", @"13", @"濟南"];    [self execSql:insertSql1];        NSString *insertSql2 = [NSString stringWithFormat:                      @"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",                      TABLENAME, NAME, AGE, ADDRESS, @"李四", @"12", @"濟南"];    [self execSql:insertSql2];    }

 

 

 

-(void) updateData{    NSString *updateSql = [NSString stringWithFormat:                      @"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",                      TABLENAME,   AGE,  @"15" ,AGE,  @"13"];    [self execSql:updateSql];}


         刪除表內容:

 

 

-(void) deleteData{    NSString *sdeleteSql = [NSString stringWithFormat:                      @"delete from %@ where %@ = '%@'",                      TABLENAME, NAME, @"張三"];    [self execSql:sdeleteSql];}


上面實現了表內容的增、改、刪,下面實現表內容查詢。

 

 

-(void) selectData{    [self openDB];    NSString *sqlQuery = [NSString stringWithFormat:                      @"SELECT * FROM %@",TABLENAME];    sqlite3_stmt * statement;        if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {                //查詢結果集中一條一條的遍曆所有的記錄,這裡的數字對應的是列值,注意這裡的列值        while (sqlite3_step(statement) == SQLITE_ROW) {            char *name = (char*)sqlite3_column_text(statement, 1);            NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];                        int age = sqlite3_column_int(statement, 2);                        char *address = (char*)sqlite3_column_text(statement, 3);            NSString *nsAddressStr = [[NSString alloc]initWithUTF8String:address];                        NSLog(@"name:%@  age:%d  address:%@",nsNameStr,age, nsAddressStr);        }    }else{        NSLog(@"select error:%@",sqlQuery);    }    sqlite3_close(db);}


 

 

 

 


@張興業TBOW

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.