Hi everyone,
I'm Updating my kind of quiz app to work with a SQLite Database.
I already implemented the SQLite Library and tried a Test Project. Know I copied my existing database to the project, and changed the path variable. I get the information, that the connection to the database worked, but I can't read data out of it.
My Read Function looks like this:
Thats my first Project with SQLite and Swift, and I feel like a total noob. Hope anybody could help me.
Thanks for Reading and best Regards.
Angelo
I'm Updating my kind of quiz app to work with a SQLite Database.
I already implemented the SQLite Library and tried a Test Project. Know I copied my existing database to the project, and changed the path variable. I get the information, that the connection to the database worked, but I can't read data out of it.
Code Block import SQLite3 class DBHelper { init() { db = openDatabase() //createTable() } let dbPath: String = "DataiOS.sqlite" var db:OpaquePointer? func openDatabase() -> OpaquePointer? { let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) .appendingPathComponent(dbPath) var db: OpaquePointer? = nil if sqlite3_open(fileURL.path, &db) != SQLITE_OK { print("error opening database") return nil } else { print("Successfully opened connection to database at \(dbPath)") return db } }
My Read Function looks like this:
Code Block func read() -> [Person] { let queryStatementString = "select QuestionID, Question from Questions;" var queryStatement: OpaquePointer? = nil var psns : [Person] = [] if sqlite3_prepare_v2(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK { while sqlite3_step(queryStatement) == SQLITE_ROW { let id = sqlite3_column_int(queryStatement, 0) let name = String(describing: String(cString: sqlite3_column_text(queryStatement, 1))) psns.append(Person(id: Int(id), name: name)) print("Query Result:") print("\(id) | \(name)") } } else { print("SELECT statement could not be prepared") } sqlite3_finalize(queryStatement) return psns }
Thats my first Project with SQLite and Swift, and I feel like a total noob. Hope anybody could help me.
Thanks for Reading and best Regards.
Angelo