首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用GRDB设置内连接

如何使用GRDB设置内连接
EN

Stack Overflow用户
提问于 2021-03-16 04:19:47
回答 1查看 147关注 0票数 0

SQLite.Swift版本可以正常工作。使用GRDB版本时,应用程序崩溃,我得到以下错误。线程1:致命错误:数据库方法不可重入。

我在GRDB版本上做错了什么?

这是SQLite.Swift版本。

代码语言:javascript
复制
static func getCommon_CategoryXX(trick_Category_XRef_Table: String, category_Table: String, trickID: Int64) -> String
{
    let trick_Type_XRef_Table = Table(trick_Category_XRef_Table)
    let type_ID_Trick_XRef = Expression<Int64>("Type_ID")
    let trick_ID_Type_XRef = Expression<Int64>("Common_ID")
    
    let itemTypse_Table = Table(category_Table)
    let itemID = Expression<Int64>("ItemID")
    let itemName = Expression<String>("Item_Name")
            
    var categoty_Name: String = ""
    
    let theQuery = trick_Type_XRef_Table.join(.inner, itemTypse_Table, on: itemID == type_ID_Trick_XRef).filter(trick_ID_Type_XRef == trickID).select(itemName)
    
    do {
        for theName in try Database.shared.databaseConnection!.prepare(theQuery)
        {
            categoty_Name = theName[itemName]
        }
    } catch {
        print("Couldn't get the category type name! \(category_Table) \(error)")
    }
    // print(categoty_Name)
    // print("Cat XRef Table \(trick_Category_XRef_Table)")
    // print("Cat Table \(category_Table)")
    
    return categoty_Name
}

这就是我为GRDB版本准备的。

代码语言:javascript
复制
static func getCommon_Category(trick_Category_XRef_Table: String, category_Table: String, trickID: Int64) -> String {

    var categoty_Name: String = ""
    
    do {
        try Database_GRDB.shared.databaseConnection!.read { db in
            categoty_Name = try (String.fetchOne(db, sql: "SELECT Item_Name FROM " + category_Table + " INNER JOIN " + trick_Category_XRef_Table + " ON ItemID = Type_ID WHERE Common_ID = ?", arguments: [trickID]) ?? "")
        }
                    
    } catch {
        print("Couldn't get the category type name! \(category_Table) \(error)")
    }
    // print(categoty_Name)
    // print("Cat XRef Table \(trick_Category_XRef_Table)")
    // print("Cat Table \(category_Table)")
    return categoty_Name
}
EN

回答 1

Stack Overflow用户

发布于 2021-03-18 09:14:28

所以我最终建立了第二个Db连接,并将其设置为全局连接。看起来工作正常。

这就是导致问题的区域。我在顶部使用原始的Db连接,在3个ModelData.getCommon_Category中使用全局连接。

代码语言:javascript
复制
    func getitem_List() -> [item_List]
    {
        var theArray = [item_List]()
        
        do {
            try Database_GRDB.shared.databaseConnection!.read { db in
                for theData in try item_List.fetchAll(db, sql: "SELECT * FROM My_items ORDER BY item_Name")
                {
                    let theType = ModelData.getCommon_Category(item_Category_XRef_Table: "item_Type_XRef", category_Table: "item_Types", itemID: theData.itemID)
                    let theStyle = ModelData.getCommon_Category(item_Category_XRef_Table: "item_Style_XRef", category_Table: "item_Styles", itemID: theData.itemID)
                    let theSupplier = ModelData.getCommon_Category(item_Category_XRef_Table: "item_Supplier_XRef", category_Table: "Manufacturers_List", itemID: theData.itemID)
                    
                    theArray.append(item_List(itemID: theData.itemID, item_Name: theData.item_Name, itemType: theType, manufacturer: theSupplier, itemStyle: theStyle, item_ForSale: theData.item_ForSale, item_Sold: theData.item_Sold, Practice_item: theData.Practice_item))
                }
            }
        } catch {
            print("Getting item list failed: \(error)")
        }
        return theArray
    }


    static func getCommon_Category(item_Category_XRef_Table: String, category_Table: String, itemID: Int64) -> String
    {
        var categoty_Name: String = ""
        
        do {
            try dbQueue_GRDB.read { db in
                categoty_Name = try (String.fetchOne(db, sql: "SELECT Item_Name FROM " + category_Table + " INNER JOIN " + item_Category_XRef_Table + " ON ItemID = Type_ID WHERE Common_ID = ?", arguments: [itemID]) ?? "")
            }
            
        } catch {
            print("Couldn't get the category type name! \(category_Table) \(error)")
        }
//         print(categoty_Name)
//         print("Cat XRef Table \(item_Category_XRef_Table)")
//         print("Cat Table \(category_Table)")
        return categoty_Name
    }

我创建了第二个Db连接作为全局连接。

代码语言:javascript
复制
var dbQueue_GRDB = DatabaseQueue()

static func openTheDB()
{
    do {
        let fileUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("SmartWare_MagicWare.sqlite")
        dbQueue_GRDB = try DatabaseQueue(path: fileUrl.path)
        
        // print("Database is open")
    } catch {
        print("Opening the Db failed! \(error)")
    }
}

这是我最初的Db连接。

代码语言:javascript
复制
class Database_GRDB
{
    static let shared = Database_GRDB()
    
    // GRDB
    public let databaseConnection: DatabaseQueue?
    
    private init()
    {
        do
        {
            let fileUrl = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("MyApp.sqlite")
            
            // GRDB
            let dbConnection = try DatabaseQueue(path: fileUrl.path)
            self.databaseConnection = dbConnection
            
        } catch {
            databaseConnection = nil
            print("Cannot connect to Database. \(error)")
        }
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66645099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档