日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

FMDB/SQLCipher数据库管理

發布時間:2024/9/20 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FMDB/SQLCipher数据库管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

  • 安裝cocopods。http://my.oschina.net/u/2418942/blog/508913。

  • 安裝 ?pod "FMDB/SQLCipher"。如果安裝有問題,可以先對cocopods進行update一下。

  • 修改FMDatabase文件。添加一個宏定義,修改2個方法。添加的代碼用_標出。把帶_的代碼刪除,即可恢復。

  • #define DB_SECRETKEY @"HPSQLDatabase"

    - (BOOL)open {

    ? ? if (_db) {

    ? ? ? ? return YES;

    ? ? }

    ?? ?

    ? ? int err = sqlite3_open([self sqlitePath], &_db );

    ? ? if(err != SQLITE_OK) {

    ? ? ? ? NSLog(@"error opening!: %d", err);

    ? ? ? ? return NO;

    ? ? }else if (err == SQLITE_OK)

    ? ? {

    ? ? ? ? [self setKey:DB_SECRETKEY];

    ? ? }

    ?? ?

    ? ? if (_maxBusyRetryTimeInterval > 0.0) {

    ? ? ? ? // set the handler

    ? ? ? ? [self setMaxBusyRetryTimeInterval:_maxBusyRetryTimeInterval];

    ? ? }

    ?? ?

    ?? ?

    ? ? return YES;

    }

    - (BOOL)openWithFlags:(int)flags {

    ? ? if (_db) {

    ? ? ? ? return YES;

    ? ? }


    ? ? int err = sqlite3_open_v2([self sqlitePath], &_db, flags, NULL /* Name of VFS module to use */);

    ? ? if(err != SQLITE_OK) {

    ? ? ? ? NSLog(@"error opening!: %d", err);

    ? ? ? ? return NO;

    ? ? } else if (err == SQLITE_OK) {

    ? ? ? ? [self setKey:DB_SECRETKEY];

    ? ? }

    ?? ?

    ? ? if (_maxBusyRetryTimeInterval > 0.0) {

    ? ? ? ? // set the handler

    ? ? ? ? [self setMaxBusyRetryTimeInterval:_maxBusyRetryTimeInterval];

    ? ? }

    ?? ?

    ? ? return YES;

    }


    4. 建立sql語句文件。databasefile.sql

    CREATE TABLE IF NOT EXISTS "databaseName" (


    "did" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,

    "name" TEXT,

    "createTime" TEXT

    )


    5. 建立SQLManager文件。SQLiteManager.h和SQLiteManager.m文件

    + (instancetype)sharedSQLiteManager {

    ? ? static SQLiteManager* manager;

    ? ? static dispatch_once_t onceToken;

    ? ? dispatch_once(&onceToken, ^{

    ? ? ? ? manager = [[SQLiteManager alloc] init];

    ? ? ? ? NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    ? ? ? ? path = [path stringByAppendingPathComponent:@"databaseName.db"];

    ? ? ? ? NSLog(@"SQLitePath ---- %@",path);

    ? ? ? ? manager.queue = [[FMDatabaseQueue alloc] initWithPath:path];

    ? ? ? ? [manager creatTable];

    ? ? });

    ? ? return manager;

    }


    - (void)creatTable {

    ? ? ? ? NSString* path = [[NSBundle mainBundle] pathForResource:@"databasefile.sql" ofType:nil];

    ? ? ? ? NSError* error;

    ? ? ? ? NSString* statement = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

    ? ? ? ? if (error != nil) {

    ? ? ? ? ? ? LogE(@"創表字符串錯誤 ----- %@",error);

    ? ? ? ? }

    ?? ? ? ?

    ? ? ? ? [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {

    ? ? ? ? ? ? if ([db executeUpdate:statement]) {

    ? ? ? ? ? ? ? ? NSLog(@"%@創表成功",sqlStr);

    ? ? ? ? ? ? }else{

    ? ? ? ? ? ? ? ? LogE(@"%@創表失敗",sqlStr);

    ? ? ? ? ? ? ? ? return;

    ? ? ? ? ? ? }

    ? ? ? ? }];

    ? ? }

    }


    - (void)openDateBase {

    ? ? NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    ? ? path = [path stringByAppendingPathComponent:@"databaseName.db"];

    ? ? FMDatabase *db =? [FMDatabase databaseWithPath:path];

    ? ? if (![db open]) {

    ? ? ? ? LogE(@"數據庫打開失敗!");

    ? ? }

    }


    - (void)closeDateBase {

    ? ? NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    ? ? path = [path stringByAppendingPathComponent:@"databaseName.db"];

    ? ? FMDatabase *db =? [FMDatabase databaseWithPath:path];

    ? ? if (![db close]) {

    ? ? ? ? LogE(@"數據庫關閉失敗!");

    ? ? }

    }



    // 獲取所有信息

    - (NSMutableArray *)loadAll {

    ? ? [self openDateBase];

    ? ? NSString* loadStatement = @"SELECT * FROM databaseName ORDER BY did ASC;";

    ? ? NSMutableArray* tempArray = [NSMutableArray array];

    ? ? [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {

    ? ? ? ? FMResultSet* result = [db executeQuery:loadStatement];

    ? ? ? ? while ([result next]) {

    ? ? ? ? ? ? Detail* detail = [[Detail alloc] init];

    ? ? ? ? ? ? detail.dId = [NSNumber numberWithInt:[result intForColumn:@"did"]];

    ? ? ? ? ? ? detail.name = [result stringForColumn:@"name"];

    ? ? ? ? ? ? detail.createTime = [result stringForColumn:@"createTime"];

    ? ? ? ? ? ? [tempArray detail];

    ? ? ? ? }

    ? ? }];

    ? ? [self closeDateBase];

    ? ? return tempArray;

    }


    // 插入數據

    - (void)insertData:(Detail*)detail{

    ?? ?

    ? ? [self openDateBase];

    ? ? NSString* insertStatement = @"INSERT INTO databaseName (name,createTime) VALUES (?,?);";

    ? ? [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {

    ? ? ? ? if ([db executeUpdate:insertStatement withArgumentsInArray:@[detail.name,getail.createTime]]) {

    ? ? ? ? ? ? NSLog(@"添加成功");

    ? ? ? ? }else{

    ? ? ? ? ? ? LogE(@"添加失敗");

    ? ? ? ? }

    ? ? }];

    ? ? [self closeDateBase];

    }


    // 更新信息

    - (void)updatedata:(Detail*)detail{

    ? ? NSString* updateStatement = @"UPDATE databaseName SET name = ? WHERE did = ?;";

    ? ? [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {

    ? ? ? ? [db executeUpdate:updateStatement withArgumentsInArray:@[detail.name,detail.dId]];

    ? ? }];

    }


    // 刪除

    - (void)deleteGroup:(Detail*)detail{

    ?? ?

    ? ? NSString* deleteStatement = [NSString stringWithFormat:@"DELETE FROM databaseName WHERE did = ?;"];

    ? ? [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {

    ? ? ? ? if ([db executeUpdate:deleteStatement withArgumentsInArray:@[detail.dId]]) {

    ? ? ? ? ? ? NSLog(@"刪除成功");

    ? ? ? ? }else {

    ? ? ? ? ? ? NSLog(@"刪除失敗");

    ? ? ? ? }

    ?? ? ? ?

    ? ? }];

    ?? ?

    }


    6. 在viewController調用

    #import "SQLiteManager.h"

    @property (strong, nonatomic) SQLiteManager *manager;

    - (void)viewDidLoad {

    ? ? [super viewDidLoad];

    ? ? SQLiteManager *manager = [SQLiteManager sharedSQLiteManager];

    ? ? self.manager = manager;

    然后就可以調用方法了。


    7. 在本地文件夾中找到數據庫,用Navicat Premium 工具打開,會提示“file is encrypted or is not a database”。這表示你的數據庫已經加密了。(如果不做加密的那步驟,在這里是可以用工具打開數據庫的)。



    8. 提示: 數據庫字段名字不能用group

    ? ?如果沒有加密建立的數據庫,想再加密,那么必須把原有數據庫刪除,不然會報錯。

    轉載于:https://my.oschina.net/u/2418942/blog/517906

    總結

    以上是生活随笔為你收集整理的FMDB/SQLCipher数据库管理的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。