首页 热点专区 小学知识 中学知识 出国留学 考研考公
您的当前位置:首页正文

swift 下使用fmdb 以及如何使用SQLCipher

2024-12-13 来源:要发发知识网

找到 下图者两个函数 在对应位置添加下面2行代码
const char* key = [@"bluescflang412#" UTF8String]; 
//StrongPassword可以修改成你自己的密码
sqlite3_key(_db, key, (int)strlen(key));

修改完成后就好了使用数据库的时候我们的数据库就是加密的啦 哈哈高兴吧
swift使用fmdb的方法 :

let documentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
//写上自己的数据库名称及路径
let path = documentsFolder.stringByAppendingPathComponent("test.sqlite")
//新建数据库
let database = FMDatabase(path: path)
//打开数据库
if !database.open() {
    print("Unable to open database")
    return
}
//新建表
if!database.executeUpdate("create table test(x text, y text, z text)",     withArgumentsInArray: nil) {
    print("create table failed: (database.lastErrorMessage())")
}
//像数据库插入数据
if!database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", withArgumentsInArray: ["a", "b", "c"]) {
    print("insert 1 table failed: (database.lastErrorMessage())")
}
//另一种插入数据的方法
if !database.executeUpdate("insert into test (x, y, z) values (?, ?, ?)", withArgumentsInArray: ["e", "f", "g"]) {
    print("insert 2 table failed: (database.lastErrorMessage())")
}
//获取数据
if let rs = database.executeQuery("select x, y, z from test", withArgumentsInArray: nil) {
while rs.next() {
let x = rs.stringForColumn("x")
let y = rs.stringForColumn("y")
let z= rs.stringForColumn("z")
print("x = (x); y = (y); z = (z)")
  }
} else {
    print("select failed: (database.lastErrorMessage())")
}
//关闭数据库
database.close()

好了 执行完后 我们可以自行打印 path 找到数据库文件路径
在命令行下执行 sqlite3 test.sqlite
执行 .table
会发现什么都没有
这就对了 证明我们的数据库已经加密了哈哈
如果是没有加密的 会显示刚才所创建的 test表
验证数据库是否加密 使用
hexdump -C plaintext.db

显示全文