菜鸟教程小白 发表于 2022-12-11 19:29:15

ios - 使用事务插入会引发错误 Sqlite.swift


                                            <p><p>我已经创建了一个数据库类:</p>

<pre><code>class Database {
    static let instance = Database()
    private let categories = Table(&#34;Category&#34;)
    private var db: Connection?

    let cat_id = Expression&lt;String&gt;(&#34;id&#34;)
    let cat_name = Expression&lt;String&gt;(&#34;name&#34;)


    private init() {
      let path = NSSearchPathForDirectoriesInDomains(
            .documentDirectory, .userDomainMask, true
            ).first!
      do {
            db = try Connection(&#34;\(path)/SalesPresenterDatabase.sqlite3&#34;)
            createTable()
      } catch {
            print(&#34;error&#34;)
      }
    }
    func createTable() {
      do{
            try self.db!.run(self.categories.create(ifNotExists: true) { table in
                table.column(self.cat_id)
                table.column(self.cat_name)
            })
      }catch{
             print(&#34;error&#34;)
      }
    }

    func addRow(table: DBTableNames, object: ) -&gt; Int64? {
      do {
         try self.db!.transaction() {
            for obj in object{
                if table.rawValue == DBTableNames.Category.rawValue{
                  let cats : CategoryObject = obj as! CategoryObject
                  let insert = self.categories.insert(self.cat_id &lt;- cats.id,
                                                      self.cat_name &lt;- cats.name)
                  try self.db!.run(insert)

                }
             }
          }
      }catch{
         print(&#34;Insert failed \(error)&#34;)
         return -1
      }
      return 1
         }
}
</code></pre>

<p>然后我继续调用我的代码,通过运行以下命令添加一行:</p>

<pre><code>    let returnValue = Database.instance.addRow(table: DBTableNames(rawValue: entity)!,
                                             object: databaseObject)
</code></pre>

<p>我的问题是它总是抛出一个错误说:</p>

<blockquote>
<p>Insert failed The operation couldn’t be completed. (SQLite.Result
error 0.) and full: cannot rollback - no transaction is active (code:
1)</p>
</blockquote>

<p>如果我再次看到此错误,我的 Mac 将会弹出窗口!</p>

<p>整个操作在后台线程中,但我也尝试了以下操作:</p>

<pre><code>    DispatchQueue.main.async{
    let returnValue = Database.instance.addRow(table: DBTableNames(rawValue: entity)!,
                                                   object: databaseObject)
}
</code></pre>

<p>没有用。这没有任何意义,因为我也尝试在事务中创建表并且效果很好。 </p>

<p>任何帮助将不胜感激!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您的 <code>func createTable()</code> 和 <code>func addRow()</code> 方法不是线程安全的。多个线程可以同时访问它。</p>

<p>在您的 Singleton 类中创建一个私有(private)串行 <code>DispatchQueue</code> 并通过此串行队列执行上述功能。这样可以防止多个线程同时访问数据库,串行队列会将并发任务排队。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用事务插入会引发错误 Sqlite.swift,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43388443/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43388443/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用事务插入会引发错误 Sqlite.swift