I need to use an existing SQLite db that is will be shared by a Python app in an iOS application.
How do I integrate the db into my project? I have a framework that's integrated into my project that acts as a controller for the app. So, I've focused on integrating the db with that. This is the code I'm trying to get to work:
import Foundation
import SQLite3
public class Board {
public static let shared = Board()
public static var db: OpaquePointer?
public init() {
Board.startDB()
}
public static func startDB() {
let fileURL = try! FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathComponent("words.db")
print(fileURL)
// open database
guard sqlite3_open(fileURL.path, &Board.db) == SQLITE_OK else {
print("error opening database")
sqlite3_close(Board.db)
return
}
print(" db ok")
return
}
it's reporting "db ok" in the console, so it's getting that far. In the ViewController, I have this as the last line in viewDidLoad()
print ("board db is \(Board.getCount())")
that calls this, but returns "no go"
public static func getCount() {
let query = "select count(*) from words;"
print(query)
var queryStatement = Board.db
if sqlite3_prepare(Board.db, query, -1, &queryStatement, nil) == SQLITE_OK{
while(sqlite3_step(queryStatement) == SQLITE_ROW){
let count = sqlite3_column_int(queryStatement, 0)
print("\(count)")
}
} else {
print("no go")
}
}
the words.db file is stored in the same directory as the board.swift file, which is the files directory for the framework. The console spits out
file:///Users/dandonaldson/Library/Developer/CoreSimulator/Devices/B03E9CD5-6311-4E3C-BDC7-43CF97ACEC24/data/Containers/Data/Application/B55E3D64-CDD0-49EB-AFE4-478305DA3DE9/Library/Application%20Support/words.db
for that, which I don't know how to interpret... I guess the first question is, do I need to tell the main project that that file is a resource of some kind, and how do I go about that? Where should it reside?
The bigger question is, generally, what's the best way to go about this? Because the database needs to be in sync with another app, the use of CoreData hasn't been pursued, and for the moment It'll be treated as not an option...