Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

macos - Saving sqlite database with swift

After doing this tutorial

http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_8_Application_using_Swift_and_FMDB

I found that the database was only created while the app was running. How could I access the database the second time I run the app. And my final goal is to create a database and use the data to display things.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I use the following simple wrapper:

private let _SingletonSharedInstance = SQLite()

class SQLite {

  private var db: COpaquePointer = nil
  private let path = "<path to your db>.rdb"
  private var statement: COpaquePointer = nil
  private let SQLITE_STATIC = sqlite3_destructor_type(COpaquePointer(bitPattern: 0))      // http://stackoverflow.com/a/26884081/1271826
  private let SQLITE_TRANSIENT = sqlite3_destructor_type(COpaquePointer(bitPattern: -1))

  class var sharedInstance : SQLite {
    return _SingletonSharedInstance
  }

  init() {
    if sqlite3_open(path, &db) != SQLITE_OK {
      fatalError("database not found")
    }
  }

  func select(query:String) -> Bool {
    if sqlite3_prepare_v2(db, "SELECT (query)", -1, &statement, nil) == SQLITE_OK { return true }
    let errmsg = String.fromCString(sqlite3_errmsg(db))
    println("error preparing select: (errmsg!)")
    return false
  }

  func prepare(query:String) -> Bool {
    if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK { return true }// "insert into test (name) values (?)"
    let errmsg = String.fromCString(sqlite3_errmsg(db))
    println("error preparing '(query)': (errmsg!)")
    return false
  }

  func bind(parm:Int, value:String) -> Bool {
    if sqlite3_bind_text(statement, Int32(parm), value, -1, SQLITE_TRANSIENT) == SQLITE_OK { return true }
    let errmsg = String.fromCString(sqlite3_errmsg(db))
    println("failure binding foo: (errmsg!)")
    return false
  }

  func execute() -> Bool {
    if sqlite3_step(statement) == SQLITE_DONE { return true }
    let errmsg = String.fromCString(sqlite3_errmsg(db))
    println("failure in execute: (errmsg!)")
    return false
  }

  func lastId() -> Int {
    return Int(sqlite3_last_insert_rowid(db))
  }

  func nextRow() -> Bool {
    if sqlite3_step(statement) == SQLITE_ROW { return true }
    statement = nil
    return false
  }

  func intAt(col:Int) -> Int {

    return Int(sqlite3_column_int64(statement, Int32(col)))
  }

  func textAt(col:Int) -> String {
    let name = sqlite3_column_text(statement, Int32(col))
    if name != nil {
      return String.fromCString(UnsafePointer<Int8>(name))!
    }
    return ""
  }

}

and then in the single classes:

let sqLite = SQLite.sharedInstance
sqLite.select("* FROM table")
while sqLite.nextRow() { 
  println(sqLite.textAt(0))
}

I guess the rest is obvious.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...