標籤:
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)
}
}
}
SQLite初試