接着我们就可以创建数据库或表了
//相当于c语言的指针
var db: OpaquePointer? = nil
// 创建或打开数据库
func createOrOpenDatabase() {
print("\(NSHomeDirectory())")
let path: NSString = "\(documentsPath)/test.sqlite3" as NSString
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)
}
}