Closed omi9430 closed 3 years ago
Hi there. I got this same error today, it means you didn't create the table. You need to create the table first.
@Gustavopcaiafa I have a pre-loaded databse
I have this same problem too. If I have a pre-loaded database(.sqlite) and I want to read from it, but always show "no such table: Content (code: 1)" when I called ``` do { for row in (try db.prepare("SELECT id, title FROM Content")) { print("id: (row[0]), title: (row[1])") } } catch { print("DBHelper read error: (error)") }
@Gustavopcaiafa I have a pre-loaded databse I tried a solution for this. Cause the db not to be read, because the behavior of dragging db to XCode and the behavior of dragging other files(mp3) have a difference, which will result in reading nil. Refer following pics:
Hope to be helpful
I'm seeing this on an In Memory Db. What's actually happening is that the connection is being nulled between statements because I'm using builders on my tests and not actually keeping a reference alive.
Beware of the GREAT ARC!
hope it will work
let directoryList = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) var documentDirectory = directoryList.first documentDirectory?.append("/yourdbname.sqlite3") print(documentDirectory!)
if !FileManager.default.fileExists(atPath: documentDirectory!) {
let databaseBundlePath = Bundle.main.path(forResource: "yourdbname", ofType: "sqlite3")
do {
try FileManager.default.copyItem(atPath: databaseBundlePath!, toPath: documentDirectory!)
self.databasePath = documentDirectory!
} catch {
print("Unable to copy database.")
}
} else {
print("database exist")
self.databasePath = documentDirectory!
}
A bit late to the discussion but just in case...
If you are making any write operations on the DB just make sure the DB file you are connecting to is not in the app bundle (you cannot manipulate files in a bundle), first you need to move them into another directory.
I ran the following command in order to make sure if everything is working fine but i got the above error. No such table (Code : 1) I am sure the table name is correct do{ for row in try ddata.prepare("SELECT id FROM Categories") { print("id(row[0])") } }catch { print(error) }
Build Information
xcode 10.3 swift 4.2 database extension is .db3
General guidelines