2

I'm trying to use sqlite.swift in a small app I'm developing, but I'm new to Swift and SQLite. I used CocoaPods to install sqlite.swift. I used these commands:

sudo gem install cocoapods
pod setup --verbose

I then navigated to the directory for my app and entered:

pod init
open -a Xcode Podfile

I then edited the pod as follows:

platform :ios, '9.0'

target 'GeneralPractice (iOS)' do
    pod 'SQLite.swift', '~> 0.13.1'
end

I then open the app workspace and add import sqlite3 to ContentView.swift, and then add this code before struct ContentView: View {:

let path = NSSearchPathForDirectoriesInDomains(
    .documentDirectory, .userDomainMask, true
).first!

let db = try Connection("\(path)/db.sqlite3")

When I run the app, I get this error:

"Cannot find 'Connection' in scope"

Given how new I am to this and to Swift, I suspect I'm making some simple error, but any help resolving this would be very much appreciated.

randombits
  • 21
  • 3
  • 1
    Have you imported the library, `import SQLite` In ContentView.swift? – Joakim Danielson Dec 18 '21 at 21:10
  • 1
    There is a `pod init`, but is there a `pod install`? Do you see `SQLite.swift` in your workspace, in the Pods folder? – Larme Dec 18 '21 at 22:22
  • My ContentView file has import sqlite3, not sqlite. When I tried to enter sqlite manually, I got an error. The Pods folder does have SQLite.swift in several lines. It does not appear as one of the apps Frameworks. – randombits Dec 18 '21 at 22:54
  • According to the docs on https://github.com/stephencelis/SQLite.swift, you need to `import SQLite`. The module itself imports sqlite3 under the hood – timbre timbre Dec 18 '21 at 23:53

1 Answers1

0

I finally solved this problem. As Kiril S. suggested, SQLite.swift only works if "import SQLite" is used. However, as far as I can tell, "import SQLite" can only be used without errors in a file that contains no Views. When I use "import SQLite" in a file with any View (like ContentView.swift) I get multiple error messages. If I create a new Swift file without any View, everything works fine.

randombits
  • 21
  • 3