0

I'm using SQLite.swift to run queries on a .db file in my Xcode project, and I keep receiving "code: 26 -- file is not a database" when I try to connect to the database from the documents folder on the device.

When I use the file path to the .db file in on my desktop, it has no problem running my queries. When I add the file to Xcode, however, it comes up with code 26 whether I try to access it from the Bundle.main path or when I copy it to the device's documents folder and try to access it there. If I use the terminal to open the database, it opens in my SQLite viewer just as it would if I accessed it in my desktop. Thus, I know it is a .db file, I know my code can work with this file, but when I try to access it on the device, it somehow doesn't recognize it as a database!

Thanks for your help. This has been plaguing me for days, and I can't figure out what the heck is going on!

  • It may be file format version. [sql db file format](https://www.sqlite.org/fileformat.html) – Ptit Xav Feb 16 '22 at 07:43
  • @PtitXav I considered that, but the code works when I access the .db file on my desktop. The issue occurs when I bring it onto the device. – Benji 勉志 Feb 16 '22 at 16:39
  • how do you open the db on device ? 26, may also be for encrypted dabases – Ptit Xav Feb 16 '22 at 17:39
  • I am using SQLite.swift to run queries on the database. I can run the queries on the simulator as long as I am specifying a file path outside of the Xcode project, but when I try to access the file within the Bundle, or if I copy it from the bundle to the device's Documents folder, it gives me code 26. I can open the database without an issue if I navigate to the Device's file path in the terminal. If it were encrypted, it apparently somehow happens after I copy the file into Xcode, as that is the only time I run into issues – Benji 勉志 Feb 16 '22 at 19:53
  • Did you try printing the full path just before the connect statement? It might be helpful to see the code around the connect. I know from experience that it is easy to mess up the file path in iOS. – Bill Feb 17 '22 at 03:46
  • @Benji勉志 , the app may not access the db in bundle because it is read only. Did you try to add a line of code in your app to copy it from bundle to app document directory before opening it? – Ptit Xav Feb 17 '22 at 06:43
  • I actually figured it out! I accidentally dragged a copy of the file to the bundle rather than the actual database. I thought the file in the bundle was referencing the file I dragged in, but it was referencing a file elsewhere that the file I dragged in was referencing! A silly mistake, but it took me a while to figure out! – Benji 勉志 Feb 17 '22 at 23:18

1 Answers1

0
below is yours answers


class SqliteDB{
    static let shared:SqliteDB = SqliteDB()
    private init(){}
    
    var DB:Connection!
    
    /// path for creat a databased file.
    func createDBPath(){
        do {
            let dbPath = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("chatDB.sqlite").path
            DB = try Connection(dbPath)
            print(dbPath)

        } catch {
            print(error.localizedDescription)
        }
    }
    
    func dbTables_ChatMessage(){
        let tableQuery = """
           CREATE TABLE IF NOT EXISTS message_list (id INTEGER, conversation_id INTEGER UNIQUE, conversation_recipient_id INTEGER, timestamp INTEGER UNIQUE, content_type TEXT, message TEXT, user_id INTEGER, user_name TEXT, group_id INTEGER,isOnline TEXT,room_id INTEGER, local_conversation_id INTEGER ,room_unique_id TEXT,PRIMARY KEY(id AUTOINCREMENT))
          """
        do {
            try DB.run(tableQuery)
        } catch  {
            print(error.localizedDescription)
        }
    }
  }

use singleton access this class objects

SqliteDB.shared.createDBPath(). // use in a--delegates when app is launch SQliteDb create a path as well as databases

SqliteDB.shared. dbTables_ChatMessage() where you want to create table