Я использую stephencelis / SQLite.swift и пытаюсь подключить 2 базы данных во время выполнения:
ATTACH DATABASE \ '(databasePath.Local) \' AS LOCAL_USER_DB;
//
// DatabaseWrapper.swift
// ...
//
import UIKit
import SQLite
class DatabaseWrapper {
// MARK: - Constants
struct databaseName {
static let Interface: String = "app_interface_sqlite3_db.db3"
static let Local: String = "app_local_sqlite3_db_local.db3"
}
struct databasePath {
static let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true
).first!
static let Interface = "\(path)/\(databaseName.Interface)"
static let Local = "\(path)//\(databaseName.Local)"
static let Interface_Bundle: String = Bundle.main.path(forResource: "app_interface_sqlite3_db", ofType: "db3") ?? ""
static let Local_Bundle: String = Bundle.main.path(forResource: "app_local_sqlite3_db_local", ofType: "db3") ?? ""
}
// MARK: Copy Databases
public static func copyDatabasesIfNeeded() {
copyDatabaseIfNeeded(databaseName: databaseName.Interface)
copyDatabaseIfNeeded(databaseName: databaseName.Local)
}
public static func copyDatabaseIfNeeded(databaseName: String) {
print("PATH: \(databasePath.Interface)")
// Move database file from bundle to documents folder
let fileManager = FileManager.default
let documentsUrl = fileManager.urls(for: .documentDirectory,
in: .userDomainMask)
guard documentsUrl.count != 0 else {
return // Could not find documents URL
}
let finalDatabaseURL = documentsUrl.first!.appendingPathComponent(databaseName)
if !( (try? finalDatabaseURL.checkResourceIsReachable()) ?? false) {
print("DB does not exist in documents folder")
let documentsURL = Bundle.main.resourceURL?.appendingPathComponent(databaseName)
do {
try fileManager.copyItem(atPath: (documentsURL?.path)!, toPath: finalDatabaseURL.path)
} catch let error as NSError {
print("Couldn't copy file to final location! Error:\(error.description)")
}
} else {
print("Database file found at path: \(finalDatabaseURL.path)")
}
}
// MARK: - Attach Local User Database TESTS
public static func attachDatabase() {
do {
// if the database connection works
let db = try Connection(databasePath.Interface);
let attachStr = "ATTACH DATABASE \'\(databasePath.Local)\' AS LOCAL_USER_DB;"
try db.execute(attachStr)
} catch {
// handle error
print("Unexpected error: \(error).")
}
}
public static func checkIfDatabase2IsAttached() -> Bool {
var isAllreadyAttached = false
do {
// if the database connection works
let db = try Connection(databasePath.Interface);
// seq, name, file
var row_counter = 0
for row in try db.prepare("PRAGMA database_list;") {
print("\n\n----------\row: \(row)\n----------\n\n")
row_counter += 1
}
isAllreadyAttached = (row_counter == 2) ? true : false
} catch {
// handle error
print("Unexpected error: \(error).")
}
return isAllreadyAttached
}
public static func testIfDatabaseWasAttached() {
do {
// if the database connection works
let db = try Connection(databasePath.Interface);
for row in try db.prepare("SELECT * FROM LOCAL_USER_DB.users;") {
print("row: \(row)")
}
} catch {
// handle error
print("Unexpected error: \(error).")
}
}
// ...
}
Когда я звоню
// Copy both databases if needed
DatabaseWrapper.copyDatabasesIfNeeded()
DatabaseWrapper.attachDatabase()
if (DatabaseWrapper.checkIfDatabase2IsAttached()) {
print("ATTACHED")
} else {
print("NOT ATTACHED YET")
}
DatabaseWrapper.testIfDatabaseWasAttached()
К сожалению, я не работаю и не получаю никаких сообщений об ошибках.
В исходном коде я получаю журнал:
---------- ow: [Необязательно (0), Необязательно («основной»), Необязательно («.... / Разработчик / CoreSimulator / Устройства / F93B5F0 C -87D0» -439 C -ACB9-B1C6B5B7BE60 / data / Containers / Data / Application / BB9587A1-2512-4003-ABC0-8AB1A7F6B4AF / Documents / app_interface_sqlite3_db.db3 ")]
НЕ УКАЗАНА, ошибка отсутствует таблица: LOCAL_USER_DB.users (код: 1).
Кто-нибудь знает, кто это делал раньше или кто-нибудь мне помогает?
Заранее спасибо.