美文网首页
Swift与数据库之SQLite的基本使用--DDL

Swift与数据库之SQLite的基本使用--DDL

作者: 20b347b28fc9 | 来源:发表于2016-03-22 22:56 被阅读254次

Swift中SQLite的使用--DDL


步骤

1. 在Xcode中导入SQLite文件

![](file:///Users/erer/Desktop/Snip剪切暂存/Snip20160315_1.png)

2.建立桥接
  • 创建一个头文件,如SQLiter+Bridge.h
  • 在BuildSetting中的配置如下

![在build Setting中配置 项目名/头文件](file:///Users/erer/Desktop/Snip剪切暂存/Snip20160315_2.png)

  • 在SQLiter+Bridge.h头文件中导入sqlite3.h
#ifndef SQlite_Bridge_h
#define SQlite_Bridge_h

//导入sqlit3.h头文件
#import "sqlite3.h"

#endif
3.创建SQLiteManager[单例]
static let shareInstance: SQLitemanager = SQLiteManager()
4.DDL创建数据库和表
class SQLiteManager: NSObject {
    //单例
    static let shareInstance : SQLiteManager = SQLiteManager()
    //0 定义数据库变量
    var db: COpaquePointer = nil
    override init() {
        
        //创建数据库文件
        
        //0-1 数据库路径
        guard let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true).first else{
            return
        }
        //拼接数据库文件名称
        let filepath = (path as NSString).stringByAppendingPathComponent("demo.sqlite")
        //转换成C语言字符串
        guard let cFilepath = filepath.cStringUsingEncoding(NSUTF8StringEncoding) else{
            return
        }
        
        sqlite3_open(cFilepath, &db)
        
        //创建表
        let sql = "CREATE TABLE IF NOT EXISTS T_Student (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, score REAL);"
        guard let csql = sql.cStringUsingEncoding(NSUTF8StringEncoding) else{
            return
        }
        
        sqlite3_exec(db, csql, nil, nil, nil)
    }
}

其中方法中具体参数意义如下


//1.创建数据库文件
/*
参数一:<#T##filename: UnsafePointer<Int8>##UnsafePointer<Int8>#> 数据库路径(C语言)
参数二:<#T##ppDb: UnsafeMutablePointer<COpaquePointer>##UnsafeMutablePointer<COpaquePointer>#> 需要保存的数据库
*/

sqlite3_open(cFilePath, &db)
        
//2.创建表
        

//1.编写sqlite语句
let sql = "CREATE TABLE IF NOT EXISTS T_Student (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, score REAL);"

//C语言
guard let cSql = sql.cStringUsingEncoding(NSUTF8StringEncoding) else{
    return
}
        
//2.执行sqlite语句
/*
参数一:<#T##COpaquePointer#>   需要执行在的数据库文件
参数二:<#T##sql: UnsafePointer<Int8>##UnsafePointer<Int8>#>   sqli语句
参数三:回调
<#T##callback: ((UnsafeMutablePointer<Void>, Int32, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) -> Int32)!##((UnsafeMutablePointer<Void>, Int32, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) -> Int32)!##(UnsafeMutablePointer<Void>, Int32, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>, UnsafeMutablePointer<UnsafeMutablePointer<Int8>>) -> Int32#>
        参数四:<#T##UnsafeMutablePointer<Void>#> 回调的第一个参数
        参数五:<#T##errmsg: UnsafeMutablePointer<UnsafeMutablePointer<Int8>>##UnsafeMutablePointer<UnsafeMutablePointer<Int8>>#> 错误信息
        */
        
sqlite3_exec(db, cSql, nil, nil, nil)
    }

相关文章

网友评论

      本文标题:Swift与数据库之SQLite的基本使用--DDL

      本文链接:https://www.haomeiwen.com/subject/myellttx.html