0
FMDB
で既存のデータベースを開きたいとします。 DBはsqlite
データベースの末尾が.db
である必要があります。SwiftでFMDBを使用してDBを開きます
マイコードは次のとおりです。 静的LET共有:DBManagerの= DBManagerの()
let databaseFileName = "/mydb.db"
var pathToDatabase: String!
var pathNSURL: NSURL!
var database: FMDatabase!
override init() {
super.init()
let documentsDirectory = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString) as String
let zwerg = documentsDirectory + databaseFileName
let pathNSURL = NSURL(fileURLWithPath: zwerg)
let pathString = documentsDirectory + databaseFileName
pathToDatabase = pathNSURL.path
print(pathToDatabase)
}
func firstQuestion() -> Bool {
if openDatabase(){
let query = "SELECT * FROM movie"
do {
let result = try database.executeQuery(query, values: nil)
print(result)
}
catch {
print(error.localizedDescription)
}
database.close()
return true
}
return false
}
func openDatabase() -> Bool {
if database == nil {
print(FileManager.default.fileExists(atPath: pathToDatabase))
if FileManager.default.fileExists(atPath: pathToDatabase) {
print("Database set new path -> File exists")
database = FMDatabase(path: pathToDatabase)
}
}
if database != nil {
print("Database != nil")
if database.open() {
print("Database is open in != nil")
return true
}
}
return false
}
方法firstQuestionを呼び出すイム()VCから:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
print(DBManager.shared.firstQuestion())
}
私の問題はfileExists-ということですメソッドは常にfalseを返します。ファイル名は正しいです。これは迅速なデータベースを持つ私の最初のプロジェクトなので、おそらく私は愚かな間違いを犯しました...どんな提案?
ありがとうございました!
:ファイル:///ユーザ/ OverBurned /ライブラリ/開発/ CoreSimulator /デバイス/ B5EAE004-A036-4BD5-A692-C25EF3875D25 /データ/コンテナ/データ/アプリケーション/ 3BC1CBA7-48E1-4A17-A456-108667E7F773 /ドキュメント/ mydb.dbしかし、それはソリューションではなかったとにかく – OverBurned
私の2番目のソリューションで私はこのエラーメッセージが表示されます:致命的なエラー:オプションの値 – OverBurned
更新が試行され、それは私のために役に立ちます – Alex