mercredi 14 octobre 2015

FMDB not inserting into sqlite on OS X

I've red a lot of Stack Overflow answers regardless to my problem but non of them helped me on OS X. What I want to do is to make hidden sqlite database where I can store some data. Database opens fine and while executing statement I couldn't find any error.

Here is code I use for creating database and inserting into it. In iOS there is a limitation that you can only write to sqlite store if it's saved inside DocumentsDirectory and I tried that solution but it didn't help.

  var documentFolderPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
  documentFolderPath = documentFolderPath.stringByAppendingString("/Hasher")

  if NSFileManager.defaultManager().fileExistsAtPath(documentFolderPath) == false {
      do {
          try NSFileManager.defaultManager().createDirectoryAtPath(documentFolderPath, withIntermediateDirectories: true, attributes: nil)
      }
      catch {
      }
  }

  let dbFilePath = documentFolderPath.stringByAppendingString("/" + self.kDatabaseInfo.fullName())

  let database = FMDatabase(path: dbFilePath)
  if database.open() {

      let createStatement = "CREATE TABLE IF NOT EXISTS cache (KEY TEXT NOT NULL, TYPE VARCHAR(5));"
      if database.executeUpdate(createStatement, withArgumentsInArray: nil) == true {
          print("Database setup successfull")
      }

      let insertStatement = String(format: "INSERT INTO %@ (KEY, TYPE) VALUES(?, ?)", self.kDatabaseInfo.tableName)
      database.executeQuery(insertStatement, withArgumentsInArray: [value, type])
      if let error = database.lastError() {
          print(error) // prints Error Domain=FMDatabase Code=0 "not an error" UserInfo={NSLocalizedDescription=not an error}
      }
      database.close()
  }

I'm running OS X 10.11 - El Capitan

Aucun commentaire:

Enregistrer un commentaire