Swift3 sqlite3_open()打开现有文件 [英] Swift3 sqlite3_open() open an existing file

查看:157
本文介绍了Swift3 sqlite3_open()打开现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let file_url = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil,create: false).appendingPathComponent("asd.db")
var db:OpaquePointer? = nil
if sqlite3_open(file_url.path, &db) == SQLITE_OK
{
    print("Successfully opened connection database!!")
    var q:OpaquePointer? = nil
    if sqlite3_prepare_v2(db, "SELECT * FROM aaa", -1, &q, nil) == SQLITE_OK
    {
        if sqlite3_step(q) == SQLITE_ROW
        {
            let res = sqlite3_column_text(q, 1)
            let name = String(cString: res!)            }
        else
        {
            print("ERROR1!!!!!")
        }
    }
    else
    {
        print("ERROR!2!!!!")
    }
}

我在程序中有名为 asd.db的数据库文件,我正在尝试打开并

I have database file named "asd.db" in the proejct and I am trying to open and read data from the database.

我不知道为什么,但是sqlite3_open无法找到我的数据库文件,因此它在没有表的情况下创建了一个新文件。

I don't know why but sqlite3_open in not able to find my database file, so it's creates new one without my tables.

我该如何解决?

推荐答案


  1. 如果您不希望SQLite创建新数据库,请使用 sqlite3_open_v2 (与 SQLITE_OPEN_READWRITE 一起使用,但不要使用 SQLITE_OPEN_CREATE 选项)而不是 sqlite3_open

  1. If you don't want SQLite to create new databases, use sqlite3_open_v2 (with SQLITE_OPEN_READWRITE, but not the SQLITE_OPEN_CREATE option) rather than sqlite3_open.

如果您运行了一次应用程序,并且文档中有一个空白数据库文件夹,删除该应用程序,然后重新安装以清除该空白数据库。

If you ran the app once and you have a blank database in your documents folder, delete the app and reinstall to get rid of that blank database.

如果您在项目中包含了数据库(并假设已将其添加到目标),则意味着将在您的捆绑包中找到该数据库。

If you included a database "in your project" (and assuming you added it to the target in question), then that means that the database will be found in your bundle.

标准过程是编写一个例程,以查看数据库是否存在于documents文件夹中;如果不存在,则在尝试执行以下操作之前,将其从捆绑包复制到documents文件夹中从文件文件夹中打开它。

The standard process is to write a routine to see if the database exists in the documents folder, and if not, copy it from the bundle to the documents folder before trying to open it from the documents folder.

或者,也可以尝试在documents文件夹中打开它,如果由于找不到该文件而失败,请将其从分发包复制到documents文件夹中,然后尝试再次。例如:

Or, alternatively, just try opening it in the documents folder, and if that fails because the file is not found, copy it from the bundle to the documents folder and try again. For example:

var db:OpaquePointer? = nil

/// Open database
///
/// - returns: Return `true` if successful; return `false` on error.

func openDatabase() -> Bool {
    do {
        let manager = FileManager.default

        let documentsURL = try manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("asd.db")

        var rc = sqlite3_open_v2(documentsURL.path, &db, SQLITE_OPEN_READWRITE, nil)
        if rc == SQLITE_CANTOPEN {
            let bundleURL = Bundle.main.url(forResource: "asd", withExtension: "db")!
            try manager.copyItem(at: bundleURL, to: documentsURL)
            rc = sqlite3_open_v2(documentsURL.path, &db, SQLITE_OPEN_READWRITE, nil)
        }

        if rc != SQLITE_OK {
            print("Error: \(rc)")
            return false
        }

        return true
    } catch {
        print(error)
        return false
    }
}


这篇关于Swift3 sqlite3_open()打开现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆