IOS 開發學習33 使用sqlite3

來源:互聯網
上載者:User

標籤:for   count   圖片   arc   closed   操作   xcode   nsbundle   markdown   

sqlite3 命令列簡單使用

sqlite3 路徑 //開啟資料庫路徑串連
select * from sqlite_master where type=”table”; //顯示全部表結構
select * from testable; //顯示某張表資料
.tables //查看錶
.help //查看協助
.quit //退出

xcode使用sqlite3步驟1.加入libsqlite3.dylib

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVuZGg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="這裡寫圖片描寫敘述" title="">

2.標頭檔
////  DbUtils.h//  smart////  Created by 謝廠節 on 15/5/12.//  Copyright (c) 2015年 WHR. All rights reserved.//#import <Foundation/Foundation.h>#import "sqlite3.h"@interface DbUtils : NSObject{    sqlite3 *db; //聲明一個sqlite3資料庫}- (NSString *)filePath;//資料庫檔案的路徑。一般在沙箱的Documents裡邊操作-(void)openDB;-(void)closeDB;-(NSMutableArray *)getAllTypes;@end

.m檔案

////  DbUtils.m//  smart////  Created by 謝廠節 on 15/5/12.//  Copyright (c) 2015年 WHR. All rights reserved.//#import "DbUtils.h"#import "KMTypes.h"#import "KMContents.h"@implementation DbUtils//開啟資料庫的方法- (void)openDB{       ///檔案是否存在    NSFileManager* fileManager = [NSFileManager defaultManager];    NSString *dbpath=[self filePath];    NSLog(@"database path:%@",dbpath);    BOOL success = [fileManager fileExistsAtPath:dbpath];    if (!success) {        NSString *resourcePath=[[NSBundle mainBundle]resourcePath];        //自己主動複製        NSString *sourceDBPath=[resourcePath stringByAppendingPathComponent:@"app.bundle/datas.sqlite"];        NSError *error;        success = [fileManager copyItemAtPath:sourceDBPath toPath:dbpath error:&error];        if(!success)            NSAssert1(0,@"資料庫附加失敗。‘%@‘.", [error localizedDescription]);        else            NSLog(@"資料庫附加成功:%@",dbpath);    }    if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK) {        sqlite3_close(db);        NSAssert(0, @"資料庫開啟失敗。");    }}- (void)closeDB{    sqlite3_close(db);}//該方法用於返回資料庫在Documents目錄中的全路徑資訊- (NSString *)filePath{    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentsDir = [paths objectAtIndex:0];    return [documentsDir stringByAppendingPathComponent:@"datas.sqlite"];}////查詢資料全部類別- (NSMutableArray *)getAllTypes{    [self openDB];    NSMutableArray *array=[NSMutableArray arrayWithCapacity:6 ];    NSString *sql = @"SELECT * FROM km_types";    sqlite3_stmt *statement;    if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK) {        while (sqlite3_step(statement) == SQLITE_ROW) {            KMTypes* k= [[KMTypes alloc]init];            int type_id = (int)sqlite3_column_int(statement,0);            int parent_id = (int)sqlite3_column_int(statement,1);            char *type_title = (char *)sqlite3_column_text(statement, 2);            int type_order = (int)sqlite3_column_int(statement,3);            int topic_count=(int)sqlite3_column_int(statement,4);            NSString *type_titleStr = [[NSString alloc] initWithUTF8String:type_title];            k.type_title = type_titleStr;            k.type_id=type_id;            k.parent_id = parent_id;            k.type_order = type_order;            k.topic_count=topic_count;            [array addObject:k];        }        sqlite3_finalize(statement);    }    [self closeDB];    return array;}@end這裡僅僅實現一個簡單的查詢功能。

IOS 開發學習33 使用sqlite3

聯繫我們

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