1.创建一个名为test的数据库
2.添加一张学生表,表里面每一条记录包括学好,姓名,分数3个字段
工程-Build Phases-Link Binary With Libraries------libsqlite3.tbd
建立桥接文件
#import <sqlite3.h>
import UIKit
class ViewController: UIViewController {
lazy var documentsPath:String={
let paths=NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)
return paths.first!
}()
var db:COpaquePointer=nil
override func viewDidLoad() {
super.viewDidLoad()
createOrOpenDatabase()
createTable()
}
}
extension ViewController{
//创建或打开数据库
func createOrOpenDatabase(){
print("(NSHomeDirectory())")
let path:NSString = "(documentsPath)/test.sqlite3"
let filename=path.UTF8String
if sqlite3_open(filename,&db) != SQLITE_OK {
print("create or open failed.......")
sqlite3_close(db)
}
}
//创建学生表
func createTable(){
let string:NSString = "create table if not exists Student(id integer primary key autoincrement,sno text,name text,score,integer)"
let sql = string.UTF8String
if sqlite3_exec(db,sql,nil,nil,nil) != SQLITE_OK{
print("create table failed......")
sqlite3_close(db)
}
}
}