Ios SQLite.swift(0.11.2版)示例代码不起作用

Ios SQLite.swift(0.11.2版)示例代码不起作用,ios,swift,sqlite.swift,Ios,Swift,Sqlite.swift,我试图使用示例代码,但它抛出了一个错误 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let db = try Co

我试图使用示例代码,但它抛出了一个错误

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let db = try Connection("path/to/db.sqlite3")

    let users = Table("users")
    let id = Expression<Int64>("id")
    let name = Expression<String?>("name")
    let email = Expression<String>("email")

    try db.run(users.create { t in
        t.column(id, primaryKey: true)
        t.column(name)
        t.column(email, unique: true)
    })

    return true
}
我所做的第一步是使用cocoapods安装它,它是成功的。然后我尝试在我的应用程序委托中测试它,我在其中声明import SQLite,并在didFinishLaunchingWithOptions中添加了以下代码,但它显示了一个错误

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let db = try Connection("path/to/db.sqlite3")

    let users = Table("users")
    let id = Expression<Int64>("id")
    let name = Expression<String?>("name")
    let email = Expression<String>("email")

    try db.run(users.create { t in
        t.column(id, primaryKey: true)
        t.column(name)
        t.column(email, unique: true)
    })

    return true
}
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:Any]?)->Bool{
//应用程序启动后自定义的覆盖点。
让db=try连接(“path/to/db.sqlite3”)
let users=表(“用户”)
设id=表达式(“id”)
让name=表达式(“name”)
让email=表达式(“email”)
尝试db.run(users.create{t in
t、 列(id,primaryKey:true)
t、 列(名称)
t、 列(电子邮件,唯一:true)
})
返回真值
}
我从这个代码行(让db=try Connection(“path/to/db.sqlite3”))得到的错误是“从这里抛出的错误不会被处理”


有没有人遇到过同样的问题?我使用的是XCode 8.2.1和Swift 3.0。

当出现运行时错误时,您的应用程序需要知道应该做什么。您缺少的是一个
do/catch
子句或
在函数声明中抛出
关键字。有关更多详细信息,请查看。

当出现运行时错误时,您的应用程序需要知道应该做什么。您缺少的是一个
do/catch
子句或
在函数声明中抛出
关键字。有关更多详细信息,请查看。

感谢@martin-r提供的解决方案。这是更新后的代码。 解决方案:

do {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

        let db = try Connection("\(documentsPath)/db.sqlite3")

        let users = Table("users")
        let id = Expression<Int64>("id")
        let name = Expression<String?>("name")
        let email = Expression<String>("email")

        try db.run(users.create { t in
            t.column(id, primaryKey: true)
            t.column(name)
            t.column(email, unique: true)
        })

    } catch {
        // Handle error
    }
do{
让documentsPath=NSSearchPathForDirectoriesInDomains(.documentDirectory、.userDomainMask,true)[0]
让db=try连接(“\(documentsPath)/db.sqlite3”)
let users=表(“用户”)
设id=表达式(“id”)
让name=表达式(“name”)
让email=表达式(“email”)
尝试db.run(users.create{t in
t、 列(id,primaryKey:true)
t、 列(名称)
t、 列(电子邮件,唯一:true)
})
}抓住{
//处理错误
}

感谢@martin-r提供的解决方案。这是更新后的代码。 解决方案:

do {
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

        let db = try Connection("\(documentsPath)/db.sqlite3")

        let users = Table("users")
        let id = Expression<Int64>("id")
        let name = Expression<String?>("name")
        let email = Expression<String>("email")

        try db.run(users.create { t in
            t.column(id, primaryKey: true)
            t.column(name)
            t.column(email, unique: true)
        })

    } catch {
        // Handle error
    }
do{
让documentsPath=NSSearchPathForDirectoriesInDomains(.documentDirectory、.userDomainMask,true)[0]
让db=try连接(“\(documentsPath)/db.sqlite3”)
let users=表(“用户”)
设id=表达式(“id”)
让name=表达式(“name”)
让email=表达式(“email”)
尝试db.run(users.create{t in
t、 列(id,primaryKey:true)
t、 列(名称)
t、 列(电子邮件,唯一:true)
})
}抓住{
//处理错误
}

看看Swift参考书中的“错误处理”一章:
try
必须在
do{}catch{}
环境中调用..谢谢@martin-r,我只是复制粘贴了github repo指南中的代码,并假设它可以工作。我会做出改变并尝试你的建议。我现在已经在上面发布了一个解决方案。谢谢你的帮助。不要在问题中发布解决方案。如果答案有帮助,你可以接受,或者发布你自己的答案。请看一下Swift参考书中的“错误处理”一章:
try
必须在
do{}catch{}
环境中调用..谢谢@martin-r,我只是从github repo指南中复制粘贴了代码,并假设它可以工作。我会做出改变并尝试你的建议。我现在已经在上面发布了一个解决方案。谢谢你的帮助。不要在问题中发布解决方案。如果答案有帮助,你可以接受,也可以发布你自己的答案。miho你对这个问题有什么看法吗?miho你对这个问题有什么看法吗?