stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.67k stars 1.56k forks source link

No such table code : 1 #952

Closed omi9430 closed 3 years ago

omi9430 commented 5 years ago

Issues are used to track bugs and feature requests. Need help or have a general question? Ask on Stack Overflow (tag sqlite.swift).

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

Gustavopcaiafa commented 5 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.

omi9430 commented 5 years ago

@Gustavopcaiafa I have a pre-loaded databse

ZihHuanShao commented 4 years ago

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)") }

ZihHuanShao commented 4 years ago

@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:

  1. https://is.gd/VwY75O
  2. https://is.gd/xDUFYR
  3. https://is.gd/lgLpYc
  4. https://is.gd/2Tc9DE

Hope to be helpful

pacu commented 4 years ago

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!

kbhamzaarif commented 4 years ago

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!
    }
rgkobashi commented 6 months ago

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.