操作步骤:
1,在 Build Phases -> Link Binary With Libraries 中点击加号,添加 libsqlite3.0.tbd 到项目中来
2,创建连接头文件Bridging-Header.h
1
2
|
# import "sqlite3.h" # import <time.h> |
3,在项目编译属性里引用头文件
4,导入SQLiteDB的代码(SQLiteDB.swift、SQLTable.swift),代码结构如下:
//获取数据库实例 db = SQLiteDB.shared //打开数据库 _ = db.openDB() //如果表还不存在则创建表(其中_id为自增主键) let result = db.execute(sql: "create table if not exists t_user(_id integer primary key,uname text,name text)")
print(result)
//查询 let data = db.query(sql: "select * from t_user") print(data) //插入 let uname = "uname" let name = "name" let sql = "insert into t_user(uname,name) values('(uname)','(name)')" let result = db.execute(sql: sql) print(result)