From 182c2d2b8e2f6f1e1ea36b014991f3c7137babcb Mon Sep 17 00:00:00 2001 From: Lisheng Guan Date: Tue, 17 Dec 2024 10:28:59 +0800 Subject: [PATCH] Refine attach and detach database --- MMEX/App/MMEXDatabase.swift | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/MMEX/App/MMEXDatabase.swift b/MMEX/App/MMEXDatabase.swift index 07502bf..105008c 100644 --- a/MMEX/App/MMEXDatabase.swift +++ b/MMEX/App/MMEXDatabase.swift @@ -62,8 +62,7 @@ extension ViewModel { defer { url.stopAccessingSecurityScopedResource() } do { // Attach the external database - let attachSQL = "ATTACH DATABASE ? AS ?" - try db.run(attachSQL, url.path, resolvedAlias) + try db.attach(.uri(url.path, parameters: [.mode(.readOnly)]), as: resolvedAlias) log.info("Successfully attached database at \(url.path) as \(resolvedAlias)") } catch { log.error("Failed to attach database: \(error)") @@ -88,10 +87,9 @@ extension ViewModel { return } // Determine the alias to detach - let resolvedAlias = alias ?? "attachedDB" // Default alias if none is provided + let resolvedAlias = alias ?? "attach" // Default alias if none is provided do { - let detachSQL = "DETACH DATABASE ?" - try db.run(detachSQL, alias) + try db.detach(resolvedAlias) log.info("Successfully detached database with alias \(resolvedAlias)") } catch { log.error("Failed to detach database: \(error)")