Grdb.swift: While opening database via DatabasePool class returns an error

Created on 18 Dec 2020  路  4Comments  路  Source: groue/GRDB.swift

What did you do?

Tried to open a database via Database pool class

What did you expect to happen?

successfully opening the database

What happened instead?

error returned

Document[64759:810555] [logging] misuse at line 131400 of [378230ae7f]
SQLite error 21: not an error - while executing SELECT * FROM sqlite_master LIMIT 1

Environment

Xcode Version 11.7 (11E801a)
platform - IOS

Demo Project

   var config = Configuration()
   config.foreignKeysEnabled = false

do{
if fileMgr.fileExists(atPath: dbPath){
print(dbPath.description)
let dbPool = try DatabasePool(path:dbPath,configuration: config)
grDB = dbPool
}
}catch{
print(error)
}
//dbPath value
dpPath = "/Users/yasitha.herath/Library/Developer/CoreSimulator/Devices/E09DEC52-728F-4E88-961A-39CB74B58BC4/data/Containers/Data/Application/D56B9BA7-A017-4606-9E6C-84E237152B95/Documents/InEightDocuments/USERS/HCC_GHA/TRAINING01.db"

invalid

All 4 comments

Hello @Yasihera,

The code you provide runs without any error, in many apps. Your issue lacks context to chew on.

Please keep on investigating. When you find a problem in GRDB, come back with a Minimal, Reproducible Example, or a clear path towards reproducibility. Thank you!

Thanks for the fast reply i will investigate more and will comment a detailed issue if i wasn't able to solve the problem.

I was able to find the issue for the above problem issue is we have to give the path like this

file:///Users/yasitha.herath/Library/Developer/CoreSimulator/Devices/E09DEC52-728F-4E88-961A-39CB74B58BC4/data/Containers/Data/Application/869CC65A-276B-4D8B-9338-114CBE74CCB7/Documents/InEightDocuments/USERS/HCC_GHA/TRAINING01.db

but unfortunately when we give the path like this there is a new error -: SQLite error 14: unable to open database file

i was able to debug into your code and was able to see what happens inside seems like in DatabasePool class in the following init method nothing executes after the line:- schemaCache: DatabaseSchemaCache() , in writer = try SerializedDatabase() method. Thanks in advance.

public init(path: String, configuration: Configuration = Configuration()) throws {
GRDBPrecondition(configuration.maximumReaderCount > 0, "configuration.maximumReaderCount must be at least 1")

    // Writer
    writer = try SerializedDatabase(
        path: path,
        configuration: configuration,
        schemaCache: DatabaseSchemaCache(),
        defaultLabel: "GRDB.DatabasePool",
        purpose: "writer")

    // Readers
    var readerConfiguration = DatabasePool.readerConfiguration(configuration)

    // Readers can't allow dangling transactions because there's no
    // guarantee that one can get the same reader later in order to close
    // an opened transaction.
    readerConfiguration.allowsUnsafeTransactions = false

    var readerCount = 0
    readerPool = Pool(maximumCount: configuration.maximumReaderCount, makeElement: {
        readerCount += 1 // protected by Pool (TODO: document this protection behavior)
        return try SerializedDatabase(
            path: path,
            configuration: readerConfiguration,
            schemaCache: DatabaseSchemaCache(),
            defaultLabel: "GRDB.DatabasePool",
            purpose: "reader.\(readerCount)")
    })

    // Activate WAL Mode unless readonly
    if !configuration.readonly {
        try writer.sync { db in
            let journalMode = try String.fetchOne(db, sql: "PRAGMA journal_mode = WAL")
            guard journalMode == "wal" else {
                throw DatabaseError(message: "could not activate WAL Mode at path: \(path)")
            }

            // https://www.sqlite.org/pragma.html#pragma_synchronous
            // > Many applications choose NORMAL when in WAL mode
            try db.execute(sql: "PRAGMA synchronous = NORMAL")

            if !FileManager.default.fileExists(atPath: path + "-wal") {
                // Create the -wal file if it does not exist yet. This
                // avoids an SQLITE_CANTOPEN (14) error whenever a user
                // opens a pool to an existing non-WAL database, and
                // attempts to read from it.
                // See https://github.com/groue/GRDB.swift/issues/102
                try db.inSavepoint {
                    try db.execute(sql: """
                        CREATE TABLE grdb_issue_102 (id INTEGER PRIMARY KEY);
                        DROP TABLE grdb_issue_102;
                        """)
                    return .commit
                }
            }
        }
    }

    setupSuspension()

    // Be a nice iOS citizen, and don't consume too much memory
    // See https://github.com/groue/GRDB.swift/#memory-management
    #if os(iOS)
    setupMemoryManagement()
    #endif
}

@Yasihera, I still don't know what's wrong in GRDB. If you run an app that misuses SQLite, it is normal that any misuse error is generated from GRDB code (since this is how the app uses SQLite). But the misuse still lies in the app code. I hope you understand this logic.

Don't shoot the messenger!

Quoting myself just above:

When you find a problem in GRDB, come back with a Minimal, Reproducible Example, or a clear path towards reproducibility. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dev-dubaisoftwaresolutions picture dev-dubaisoftwaresolutions  路  4Comments

cody1024d picture cody1024d  路  7Comments

chiliec picture chiliec  路  7Comments

mtissington picture mtissington  路  6Comments

gverdouw picture gverdouw  路  6Comments