简体   繁体   English

SQLite.swift 框架

[英]SQLite.swift framework

This function not working !!!这个功能不行!!! I can add some other functions if your want I got problem with saving, its not working如果你想要我可以添加一些其他功能我在保存时遇到问题,它不起作用

Output - "Connected Ok The operation couldn't be completed. (SQLite.Result error 0.)"输出 - “Connected Ok 操作无法完成。(SQLite.Result 错误 0。)”

let db = try Connection("\(path)/database.sqlite3")
print("Connected Ok")

let nameTemp1 = Expression<String>("namePoint")
let latTemp1 = Expression<Double>("lat")
let longTemp1 = Expression<Double>("long")
let addressTemp1 = Expression<String?>("address")
let id = Expression<Int64>("id")
try db.run(TempPointOriginal.create(ifNotExists: true) { t in
    t.column(id, primaryKey: true)
    t.column(nameTemp1)
    t.column(latTemp1)
    t.column(longTemp1)
    t.column(addressTemp1)
})

try db.run(TempPointOriginal.insert(/*or: .replace,*/ nameTemp1 <- name1))
try db.run(TempPointOriginal.insert(/*or: .replace,*/ latTemp1 <- lat1))
try db.run(TempPointOriginal.insert(/*or: .replace,*/ longTemp1 <- long1))
try db.run(TempPointOriginal.insert(/*or: .replace,*/ addressTemp1 <- address1))

Two things you need to fix or at least consider with your code.您需要修复或至少考虑您的代码的两件事。 When you create a column like this当您创建这样的列时

let nameTemp1 = Expression<String>("namePoint")

you are saying it is mandatory (not null), to make it optional the type has to be optional, <String?>您是说它是强制性的(不是 null),要使其可选,类型必须是可选的, <String?>

let nameTemp1 = Expression<String?>("namePoint")

So this is something you might want to change in your table所以这是你可能想在你的表中改变的东西

Secondly your insert is wrong since you are actually performing 4 inserts but only one column at a time has been given a value and with all columns mandatory this generates an error.其次,您的插入是错误的,因为您实际上执行了 4 次插入,但一次只有一列被赋予了一个值,并且所有列都是强制性的,这会产生错误。 So unless you make your columns optional all values must be given for an insert statement因此,除非您将列设为可选,否则必须为插入语句提供所有值

try db.run(TempPointOriginal.insert(nameTemp1 <- name1, 
                                    latTemp1 <- lat1, 
                                    longTemp1 <- long1, 
                                    addressTemp1 <- address1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM